Deepak Mittal
Deepak Mittal

Reputation: 603

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

I need to find out the physical path of a grails GSP file.

My requirement is that I want to create a new layout file at run-time and use that in the application.

I have been able to achieve this without problem when the application runs on jetty (grails run-app), however, when I deploy the app on Jboss, the path at which the file needs to be created changes.

So, ideally I would like to find out at runtime using some magical utility the path of a particular GSP (lets say main.gsp layout file) and I need to create my new layout in the same directory in which main.gsp reside.

Any pointers?

-Deepak

Upvotes: 4

Views: 8859

Answers (3)

Wanderson Santos
Wanderson Santos

Reputation: 2957

There's a definitive way...

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

Out of controllers (ex. bootstrap)? Just inject..

def grailsApplication

Best regards!

Upvotes: 2

AA.
AA.

Reputation: 4606

In controllers you can use

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

Upvotes: 1

Siegfried Puchbauer
Siegfried Puchbauer

Reputation: 6539

Here is some groovy code to lookup the layout-folder path in the production environment (eg. 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 will need to restart the web-application or the container, since the views are chached in production mode.

Upvotes: 9

Related Questions