How to find out programmatically if a view or layout file exists in grails

I want to know programmatically if a view or layout exists in grails.

I think you will get an absolute epp and ask for File.exists, but I don’t know how to get this path for each environment.

I tried groovyPagesTemplateEngine.getUriWithinGrailsViews('a-view.gsp')without success.

Can you give me any pointer?

early

+5
source share
3 answers

I see two possibilities

Search file view

If you create a war file, you will see that the views are stored in WEB-INF / grails-app / views. You can find this resource.

def uri = this.getClass().getResource("/grails-app/views/...").toURI()
if(new File(uri).exists()){...}

Use PathMatchingResourcePatternResolver

assertView GrailsUrlMappingsTestCase.

def patternResolver = new PathMatchingResourcePatternResolver()
def pathPattern = "grails-app/views/" + ((controller) ? "$controller/" : "") + "${view}.*"
if (!patternResolver.getResources(pathPattern)) {...}
+3

Grails 2.0 GrailsConventionGroovyPageLocator:

GrailsConventionGroovyPageLocator groovyPageLocator

groovyPageLocator.findViewByPath(...)
groovyPageLocator.findTemplateByPath(...)

, .

+15

, , grailsAttributes (. docs GrailsApplicationAttributes). :

private templateExists(String name) {
    def template = grailsAttributes.getTemplateUri(name, request)
    def resource = grailsAttributes.pagesTemplateEngine
                                   .getResourceForUri(template)
    return resource && resource.file && resource.exists()
}

, , , , , .

+3

All Articles