gpt4 book ai didi

Grails:如何从 Controller 测试 gsp/模板文件是否存在?

转载 作者:行者123 更新时间:2023-12-02 22:30:19 25 4
gpt4 key购买 nike

我想实现以下内容:

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"
}
}

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

最佳答案

你可以试试这个:

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

if ( resource && resource.file && resource.exists() ) {
// exists
}

(修改自 this post )但请注意 getResourceForUri() 是私有(private)的( see here )。该代码对我有用,但它没有给我一种温暖/模糊的感觉。

我认为问题是根据更大的目标/问题来构建你的问题,并尝试解决它。 Grails 可能有答案。

关于Grails:如何从 Controller 测试 gsp/模板文件是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1995253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com