0

我用 rest-profile 构建了一个 grails 4.0.10 应用程序。

然后添加了控制台插件。(因为太方便了) compile 'org.grails.plugins:grails-console:2.1.1'

这依赖于gsps。 compile "org.grails.plugins:gsp" // console plugin needs to render gsps.

添加 gsp 插件后,404 处理程序现在呈现默认的 notFound.gsp 而不是 notFound.gson 文件。

有没有办法设置 URL 映射以默认呈现 .gson 视图而不是 .gsp 视图?

我发现的唯一机制是创建一个错误控制器来手动处理它,但我觉得我忽略了 UrlMappings 中非常简单的东西。

class UrlMappings {

    static mappings = {
        ...
        "404"(controller: "error", action:'notFound')
    }
}

然后创建一个 errorController 来渲染视图。

class ErrorController {
    static responseFormats = ['json', 'xml']
    
    def notFound() {
        render(view: "/notFound")
    }
}

这里希望 "404"(view: '/notFound', pleaseUseGson: true)设置我还没有找到。

4

1 回答 1

0

系统变得混乱,因为缓存插件中隐藏了一个 notFound.gsp 条目。

views.properties 文件有这一行

/WEB-INF/grails-app/views/notFound.gsp=gsp_cachenotFound_gsp

我不完全确定为什么缓存插件的这个视图在我的应用程序之前使用,但这是以后的问题。

通过将 notfound.gson 文件重命名为其他名称似乎可以规避该问题。

所以改变

"404"(view: '/notFound')

"404"(view: '/notFound404')

并将文件 notFound.gson 重命名为 notFound404.gson。

于 2021-04-29T19:43:00.270 回答