Grails:如何从控制器测试gsp /模板文件的存在?

fab*_*474 2 grails groovy internationalization

我想实现以下内容:

import org.springframework.web.servlet.support.RequestContextUtils as RCU

class HomeController {
    def home = {
        def locale = RCU.getLocale(request)
        render view: viewExists("home_$locale") ? "home_$locale": "home"
    }
}
Run Code Online (Sandbox Code Playgroud)

boolean viewExists(String viewPath)如果输入参数指向有效的GSP或模板文件,那么返回true 的代码是什么?

Mic*_*ter 6

你可以试试这个:

def uri = "test123.gsp"        
def resource = grailsAttributes.pagesTemplateEngine.getResourceForUri(uri)

if ( resource && resource.file && resource.exists() ) {
    // exists
}
Run Code Online (Sandbox Code Playgroud)

(从这篇文章修改)但请注意getResourceForUri()是私有的警告(见这里).代码对我有用,但它没有给我一种温暖/模糊的感觉.

我认为问题是根据更大的目标/问题构建您的问题,并尝试解决这个问题.Grails可能有一个答案.