How to find the physical path to a GSP file in a deployed grails application

I need to know the physical path of the GSP grails file.

My requirement is that I want to create a new layout file at runtime and use it in the application.

I was able to achieve this without problems when the application runs on the dock (grails run-app), however, when I deploy the application to Jboss, the path to the file must be changed.

So, ideally, I would like to find out at runtime, using some magic utility, the path to a specific GSP (say, the main.gsp layout file), and I need to create a new layout in the same directory that main.gsp lives in.

Any pointers?

-Deepak

+4
source share
3 answers

Below is the groovy code for finding the path to the layout folder in the production environment (for example, when deployed as war on jboss):

import org.codehaus.groovy.grails.commons.ApplicationHolder ... File layoutFolder = ApplicationHolder.application.parentContext.getResource("WEB-INF/grails-app/views/layouts").file def absolutePath = layoutFolder.absolutePath println "Absolute Path to Layout Folder: ${absolutePath}" File newLayoutFile = new File(layoutFolder, "foo.gsp") 

To use the newly created layout, you probably need to restart the web application or container because the views are processed in production mode.

+9
source

There is definitely ...

 grailsApplication.parentContext.getResource("WEB-INF/grails-app/views/path/to/my.gsp").file.toString() 

Outside of controllers (e.g. bootstrap)? Just enter ..

 def grailsApplication 

Yours faithfully!

+2
source

In controllers you can use

 grailsAttributes.getApplicationContext().getResource("/relative/path/").getFile().toString() 
+1
source

All Articles