【问题标题】:Grails 2.3.2/.3 Upgrade - unable to map URLs ending in .htmlGrails 2.3.2/.3 升级 - 无法映射以 .html 结尾的 URL
【发布时间】:2013-11-25 01:01:22
【问题描述】:

在将 grails 应用程序从 2.2.2 版本升级到 2.3.2 并最终升级到 2.3.3 后,我注意到一些以前可以工作的链接现在返回 404 状态。

为了说明,这里是示例链接和最初在 v2.2.2 中工作的相应 URL 映射条目:

http://localhost:7080/pages/mytestpage
http://localhost:7080/pages/mytestpage.html

UrlMappings.groovy

static mappings = {
    "pages/mytestpage"(controller: 'testController', action: 'testAction')
}

升级后,在给定的链接中,下面的链接不再起作用(即带有.html的链接):

http://localhost:7080/pages/mytestpage.html

解决此问题的一种方法是将 URLMappings 条目更改为如下:

UrlMappings.groovy(修改)

static mappings = {
    "pages/mytestpage(.$format)?"(controller: 'testController', action: 'testAction')
}

我的问题是,有没有办法解决这个问题而无需更新 URLMappings 条目?任何能解释这种映射在 2.2.2 版中实际工作的人也将是一个很大的帮助。谢谢!

更新

Using (.html)? 而不是 (.$format)? 在 UrlMappings.groovy 中同样有效,并且已被实际使用。

另外,在这个例子中,应用服务器直接被访问,没有使用网络服务器。

【问题讨论】:

    标签: grails grails-2.0


    【解决方案1】:

    在 Grails 2.2.x 中,grails.mime.file.extensions = true 设置和 grails.mime.types 控制 URL 中的扩展名。基本上,Grails 忽略了 mime 类型中列出的扩展,并相应地将 url 映射到控制器。(这就是为什么 mytestpage.html 有效,而不是 mytestpage.exe 或 mytestpage.anything)

    上述行为似乎是 Grails 2.3.x+ 中的更改,以支持 REST 改进。甚至URLMappings.groovy 中的默认映射也发生了相应的变化

    //Grails 2.2.x
    "/$controller/$action?/$id?"{
        constraints {
            // apply constraints here
        }
    }
    

    //Grails 2.3.x
    "/$controller/$action?/$id?(.${format})?"{
        constraints {
            // apply constraints here
        }
    }
    

    您的解决方案似乎是解决问题的正确方法

    【讨论】:

    • 您的回答帮助我更好地理解了这个概念。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    相关资源
    最近更新 更多