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

前端 未结 3 327
予麋鹿
予麋鹿 2021-01-12 22:53

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

I am thinking in obtain the absolutepath and ask for File.exists but I don\'t know how to obt

3条回答
  •  伪装坚强ぢ
    2021-01-12 23:46

    I see 2 possibilities

    Search for view file

    If you build a war file you will see that views are stored in WEB-INF/grails-app/views. You can search for that resource.

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

    Use PathMatchingResourcePatternResolver

    Find a inspiration in assertView method of GrailsUrlMappingsTestCase.

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

提交回复
热议问题