Grails views: Controller and Action name

In a project I am working on i have a navigation bar and depending on what “page” I am on I need to have the class set as active.

If you look at the web layer documentation you will see that within the scope of a GSP there are a number of pre-defined variables, including:

There are also two variables that are available that will help us out with our little problem. These variables will give us the current controller and action.

  • controllerName
  • actionName

Now in our navigation bar code we can do a simple check to see if this menu item should have the active class.

In my case I actually have a home controller. When you fire up a default grails application you won’t. This is perfectly fine and thankfully the controllerName variable will return null if this is the case. So we can modify our code to test for that.

Categories: Grails

About The Author

My name is Dan Vega and I am a Software Developer based out of Cleveland OH. I love to play with new technologies and write about my experiences here. When I am not busy being a full time geek I love to lift heavy weights and hang out with friends and family. If you have any questions please don't hesitate to contact me.

Follow me on:
  • Grant

    Nice! I really like this solution. Keeps the navigation gsp nice and DRY.