【问题标题】:Grails: How do I map a 404 with /**Grails:如何使用 /** 映射 404
【发布时间】:2012-11-11 16:35:36
【问题描述】:

我尝试为未找到的 URL 创建自定义 404 url​​ 映射:

"/test" {
    controller="test"
}
"404" {
    controller="application"
    action="send404"
}
"500" {
    controller="application"
    action="send500"
}

但由于某种原因,控制器和动作永远不会被调用。我得到默认的容器 404 页面。所以,我尝试了:

"/test" {
    controller="test"
}
"/**" {
    controller="application"
    action="send404"
}
"500" {
    controller="application"
    action="send500"
}

这似乎工作正常,除了它似乎也对每个请求调用 send404 操作。例如,如果我点击 /test,我会看到测试页面,但我也会得到我在 send404() 操作中所做的日志语句。

赞赏的想法...

【问题讨论】:

  • 我还注意到,如果我将 println() 语句添加到 TestController.index() 和 ApplicationController.send500() 操作,然后点击 /test,这两个操作都会打印到控制台相同的要求。很奇怪。操作中的 return() 语句没有帮助...
  • 只要确保您查看了来自grails.1312388.n4.nabble.com/…的所有可能性
  • 大括号而不是括号?据我所见并仅使用括号,从未使用过闭包。你试过括号吗?

标签: grails action http-status-code-404 url-mapping wildcard-mapping


【解决方案1】:

您是否尝试过在声明中删除空格,如 this answer 中所述?

"404"(controller:'application', action:'send404')

还有一个关于此主题的未决问题GRAILS-4232

【讨论】:

  • 是的,没有这样的运气。不过感谢您的回答。
  • 我用这种方式定义了它 "/"{ controller="login" action ="index" } "500"(view:'/error') 它对我有用。以这种方式定义后,请确保执行 grails clean。您也可以将这些语句添加到末尾。
  • 事实上,这就是为什么“404”不起作用的问题,但这并没有阻止 /** 起作用。事实证明,每次都请求一个该死的 favicon.ico。请参阅下面的帖子。
【解决方案2】:

在 grails 中,有一个 ErrorController,用于在 500 上渲染堆栈跟踪,等等。

class UrlMappings {
  static mappings {
    "403" (controller: "error", action: "forbidden")
    "404" (controller: "error", action: "notFound")
    "500" (controller: "error", action: "internalError")
  }
}

然后,您可以在另一个控制器中render(controller:"error", action"notFound") 以保持 RESTful。或者它会自动渲染错误控制器的 notFound 动作。

更多细节在这里:http://groovy.dzone.com/articles/grails-exception-handling-http

【讨论】:

  • 是否有实际的 ErrorController.groovy?据我了解,我们必须手动创建一个 ErrorController。
【解决方案3】:

也许是浏览器在每次请求时请求的 favicon.ico 导致这种情况发生。

// Route 404 to this action to see!
def send404() {

    log.error("404 Page Not Found! " + request.forwardURI)
    response.status = 404;
    render(view:"/application/not-found.gsp")        
}

【讨论】:

    猜你喜欢
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    相关资源
    最近更新 更多