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.