首页 > 解决方案 > Grails 4.1 - URL 映射中的静态映射在升级后无法正常工作

问题描述

我目前正在升级到 Grails 4.1。在过去的版本中,我的 URL Mappings.groovy 中有一个静态映射,如下所示:

  class UrlMappings {

      static mappings = {
          name tool: "tool/$controller/$action?/$id?"{
             "/$controller/$action?/$id?"{
                constraints {
                    // apply constraints here
                }
             }
         }

      "/"(controller: "auth", action: "login")
      "500"(view:'/error')
      "404"(view:'/notFound')
     }
  }

这在以前的 Grails 版本中运行良好,当我单击链接重定向到 urllocalhost:8000/tool/converters/list时,转换器将被识别为控制器,列表将被识别为操作,并显示正确的视图。现在我已经升级了,当我单击链接时,它重定向到的 urllocalhost:8080/tool%2Fconverters/list和错误消息“此页面不工作”是视图中显示的内容。“%2F”以某种方式被插入到 url 中并导致页面不显示。

我查看了 Grails 4 文档,但没有看到任何迹象表明 URL 映射中的静态映射格式已更改。有谁知道为什么会发生这种情况以及我该如何解决?

标签: grailsgrails-4

解决方案


请参阅https://github.com/jeffbrown/rookycodermapping上的项目。

https://github.com/jeffbrown/rookycodermapping/blob/0b7ff27a7fc8c1c1f7b4cf3dc14430ca1cac7be5/grails-app/controllers/rookycodermapping/SchemaController.groovy

package rookycodermapping

class SchemaController {

    def show() {
        render 'This is being rendered by the show action in SchemaController.'
    }
}

https://github.com/jeffbrown/rookycodermapping/blob/0b7ff27a7fc8c1c1f7b4cf3dc14430ca1cac7be5/grails-app/controllers/rookycodermapping/UrlMappings.groovy

package rookycodermapping

class UrlMappings {

    static mappings = {
        name tool: "/tool/$controller/$action?/$id?" {}

        "/"(view:"/index")
        "500"(view:'/error')
        "404"(view:'/notFound')
    }
}

https://github.com/jeffbrown/rookycodermapping/blob/0b7ff27a7fc8c1c1f7b4cf3dc14430ca1cac7be5/grails-app/views/index.gsp#L56-L59

        <p>
            Click <g:link action="show" controller="schema">here</g:link> to invoke the show action (g:link action="show" controller="schema").
            Click <g:link uri="/tool/schema/show">here</g:link> to invoke the show action (g:link uri="/tool/schema/show").
        </p>

这两个链接似乎都按预期工作。


推荐阅读