gpt4 book ai didi

Grails 全局错误处理程序

转载 作者:行者123 更新时间:2023-12-03 14:42:32 25 4
gpt4 key购买 nike

我在 UrlMappings.groovy 中定义了以下全局错误处理程序

    "404"(controller: "error", action: "notFound")
"500"(controller: "error", action: "serverError")

处理程序是这样实现的:

class ErrorController {

def notFound() {
flash.msg = "Not Found"
redirect uri: '/'
}

def serverError() {
flash.msg = "Oops"
redirect uri: '/'
}
}

当发生 404 错误时,一切正常,但当发生 500 错误时,当我重定向到 / 时,flash 范围为空。在 500(由服务器上未捕获的异常引起)后应清除闪存范围是否有某种原因?

最佳答案

你确定你只有一个 500 错误代码的映射吗?

确保在 UrlMappings.groovy 中只有一个 500 的映射(删除或注释掉 Grails 在 create-app 上提供的默认映射)

static mappings = {
"404"(controller: "error", action: "notFound")
"500"(controller: "error", action: "serverError")

"/"(view:"/index") //or whichever view you have for root context.
//"500"(view:'/error') [Culprit]
}

它应该按预期使用上述 url 映射。

实际上放置很重要,如果您将 500 的自定义映射放置在 grails 提供的默认映射之后,那么您会看到预期的结果。最后一个映射覆盖旧映射。

关于Grails 全局错误处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18322420/

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