Wednesday, April 11, 2012

Grails: Accessing paths, properties and URLs


In Service classes:

·         ApplicationHolder.application.metadata.'relative.dir'  -->  returns "relative.dir" property value from application.properties

·         ApplicationHolder.application.getMainContext().getResource('/').getFile().getAbsolutePath() -->  returns the local root directory of the webapp on the server

·         [inject] LinkGenerator grailsLinkGenerator  (literally)

o   grailsLinkGenerator.serverBaseURL + "/path"  -->  http://localhost:9999/context/path

o   grailsLinkGenerator.link(uri: "/path", absolute: true)  -->  same as above (see javadoc on link())

 

 

In gsp:

·         grailsApplication.metadata.'relative.reports.dir' -->  returns "relative.dir: property value from application.properties

·         request.getSession().getServletContext().getRealPath("/")  -->  returns the local root directory of the webapp on the server

·         request.getSession().getServletContext().getRealPath("/path")  -->  returns /path relative to the local root directory of the webapp on the server