0

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?

  • Why is it that you want the `"/$controller/$action?/$id?"` definition inside of the `name tool: "tool/$controller/$action?/$id?"` definition? – Jeff Scott Brown Jan 07 '20 at 20:57
  • @JeffScottBrown In our system, Tool is the module that a series of actions happen under. However, this module pulls all its actions from a number of other controllers. So the `name tool: "tool/$controller/$action?/$id?`, from its behaviour, I assume acts like a redirect. For example, another action under the Tool module would be written as `tool/schema/show` where schema is the controller and show is the action in the SchemaController.groovy. I assume the person who wrote it did it this way to avoid having to repeat code from other controllers in the ToolController.groovy. – No Longer Rooky Coder Jan 07 '20 at 21:19
  • "So the name tool: "tool/$controller/$action?/$id?, from its behaviour, I assume acts like a redirect." - It does not act like a redirect. No redirect would be involved unless the action that the url mapping is associated with it has code in it to issue a redirect, but that wouldn't really have anything to do with the mapping. – Jeff Scott Brown Jan 07 '20 at 21:40

1 Answers1

1

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.

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • Thanks! This worked perfectly. From the first link example "Click here", I was able to get the uri to show as `tool/schema/show` by adding `mapping="tool"`. – No Longer Rooky Coder Jan 08 '20 at 14:29