简体   繁体   English

Grails 404的URLMappings无法正常工作

[英]Grails URLMappings for 404 doesn't work properly

I have a strange problem with handling 404 HTTP response in grails 1.3.6 (the same wrong behavior was in 1.3.5). 我在grails 1.3.6中处理404 HTTP响应时遇到了一个奇怪的问题(在1.3.5中存在相同的错误行为)。 It sometimes works but most of the time it doesn't. 有时它可以工作,但大多数时候都没有。 I think it is a grails bug but haven't found any bug in grails' jira.. whenever I request for bad URL I receive default Tomcat 404 page. 我认为这是一个grails错误,但是在grails的jira中没有发现任何错误。每当我请求错误的URL时,都会收到默认的Tomcat 404页面。 My Configuration/UrlMappings.groovy looks like: 我的Configuration / UrlMappings.groovy看起来像:

class UrlMappings {

    static mappings = {
        "404" {
            controller = 'customError'
            action = 'index'
            code = 404
        }
        "500" {
            controller = 'customError'
            action = 'index'
            code = 500
        }

        "/"(controller: "home", action: "index")
        "/$controller/$action?/$id?"{
            constraints {
            // id has to be a number
                id(matches: /\d+/)
            }
        }
    }
}

Doesn anybody know how to solve it?:-) 有人知道如何解决吗?

Best, Mateo 最好,Mateo

I think the problem is you are using braces { } instead of using parenthesis ( ). 我认为问题在于您使用的是花括号{}而不是括号()。 This is how it should be listed for your example of using a customError controller. 对于使用customError控制器的示例,应该以这种方式列出它。

static mappings = { 静态映射= {

"404"(controller: "customError", action: "index") “ 404”(控制器:“ customError”,操作:“ index”)

"500"(controller: "customError", action: "index") “ 500”(控制器:“ customError”,操作:“ index”)

... ...

} }

Please see [6.4.4 in the Grails documentation][1] for more information. 有关更多信息,请参见[Grails文档中的[6.4.4]]。

[1]: http://grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.4.4 Mapping to Response Codes [1]: http : //grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.4.4映射到响应代码

Try it without the space. 尝试没有空间。 There was a bug in older versions of Grails where a space would cause the error mappings not to be picked up due to a bug in some regex somewhere: 较早版本的Grails中存在一个错误,其中的空格会由于某些正则表达式中的某个错误而导致错误映射无法被获取:

static mappings = {
    "404"{
          controller = 'customError'
           action = 'index'
        code = 404
    }
    "500"{
        controller = 'customError'
        action = 'index'
        code = 500
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM