【问题标题】:Grails URLMappings for 404 doesn't work properly404 的 Grails URLMappings 无法正常工作
【发布时间】:2011-02-07 20:08:47
【问题描述】:

我在 grails 1.3.6 中处理 404 HTTP 响应时遇到了一个奇怪的问题(在 1.3.5 中出现了同样的错误行为)。它有时有效,但大多数时候无效。我认为这是一个 grails 错误,但在 grails 的 jira 中没有发现任何错误。 每当我请求错误的 URL 时,我都会收到默认的 Tomcat 404 页面。 我的 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+/)
            }
        }
    }
}

有人知道怎么解决吗?:-)

最好, 马特奥

【问题讨论】:

    标签: grails


    【解决方案1】:

    我认为问题在于您使用大括号 { } 而不是括号 ( )。这就是您使用 customError 控制器的示例应该如何列出它。

    静态映射 = {

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

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

    ...

    }

    有关详细信息,请参阅 [Grails 文档中的 6.4.4][1]。

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

    【讨论】:

    • 有了这个方括号 {},它有时也可以工作。使用普通括号,它似乎每次都有效。问题是我必须将代码参数发送到控制器中。你知道如何使用普通括号发送它。 “params:[...]”语句不起作用...
    【解决方案2】:

    在没有空格的情况下尝试一下。在旧版本的 Grails 中存在一个错误,由于某处的某个正则表达式中的错误,空格会导致错误映射无法被拾取:

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

    【讨论】:

      猜你喜欢
      • 2012-07-21
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      相关资源
      最近更新 更多