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:
- application - The javax.servlet.ServletContext instance
- applicationContext The Spring ApplicationContext instance
- flash - The flash object
- grailsApplication - The GrailsApplication instance
- out - The response writer for writing to the output stream
- params - The params object for retrieving request parameters
- request - The HttpServletRequest instance
- response - The HttpServletResponse instance
- session - The HttpSession instance
- webRequest - The GrailsWebRequest instance
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.
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.