2008-11-26

Groovy Grails and your URL

Getting the URL you are at in Grails is pretty easy. Just use the dependency injected properties inside your controller and some of the nifty Grails built-in helpers.

For example, want to know the application you are running in? Each grails application controller will be able to grab this info from the grailsApplication object magically available to it via the power of dependency injection. That grailsApplication object will hold metadata about the project which is the info stored in the file application.properties...

def warName = grailsApplication.metadata.'app.name'

Want to know your controller? Each controller has the variable "controllerName" already for you to use.

So you could build a back link to your current controller using the request object and metadata like so:

def urlString = request.scheme + "://" + request.serverName
+ ":" + request.serverPort
+ "/" + grailsApplication.metadata.'app.name'
+ "/" + controllerName + "/"