Search code examples
grailsgrails-4

Grails 4.1 - Static Mapping in URL Mappings not working properly after upgrade


I am currently upgrading to Grails 4.1. In the past version I had a static mapping in my URL Mappings.groovy as follows:

  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')
     }
  }

This was working perfectly in previous Grails versions and when I click on a link to redirect to the url localhost:8000/tool/converters/list, converter would be recognized as the Controller, list would be recognized as the Action and the correct view would be displayed. Now that I have upgraded, when I click on the link, the url that it redirect to is localhost:8080/tool%2Fconverters/list and the error message "This page isn't working" is what is displayed in the view. The "%2F" somehow gets inserted into the url and is causing the page to not display.

I have looked at the Grails 4 documentation and I don't see any indication that the format for static mappings in the URL Mappings has changed. Does anyone have any idea as to why this is happening and how I can fix it?


Solution

  • See the project at 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>
    

    Both of those links appear to work as expected.