9

I am working with Grails 2.1.1 and would like to add a handful of customized URLs that map to Controller Actions.

I can do that, but the original mapping still works.

For example, I created a mapping add-property-to-directory in my UrlMappings as follows:

class UrlMappings {

    static mappings = {
        "/add-property-to-directory"(controller: "property", action: "create")
        "/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }

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

Now, I can hit /mysite/add-property-to-directory and it will execute PropertyController.create, as I would expect.

However, I can still hit /mysite/property/create, and it will execute the same PropertyController.create method.

In the spirit of DRY, I would like to do a 301 Redirect from /mysite/property/create to /mysite/add-property-to-directory.

I could not find a way to do this in UrlMappings.groovy. Does anyone know of a way I can accomplish this in Grails?

Thank you very much!

UPDATE

Here is the solution that I implemented, based on Tom's answer:

UrlMappings.groovy

class UrlMappings {

    static mappings = {

        "/add-property-to-directory"(controller: "property", action: "create")
        "/property/create" {
            controller = "redirect"
            destination = "/add-property-to-directory"
        }


        "/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }

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

RedirectController.groovy

class RedirectController {

    def index() {
        redirect(url: params.destination, permanent: true)
    }
}
angryip
  • 2,140
  • 5
  • 33
  • 67
Philip Tenn
  • 6,003
  • 8
  • 48
  • 85
  • 1
    Its not possible as of now. There's a feature request for making it possible to specify redirects in url mappings - See http://jira.grails.org/browse/GRAILS-5994 – Sudhir N Sep 27 '12 at 05:53
  • @sudhir Thank you, that answers my question. Could you please copy your comment to an Answer so I can accept it? – Philip Tenn Sep 27 '12 at 13:55
  • @sudhir, thanks for your comment and the useful link ... Tom edited his Answer and it led me on the right track to do what was looking for so I accepted his answer. – Philip Tenn Sep 27 '12 at 18:54

3 Answers3

4

It is possible to achieve this:

"/$controller/$action?/$id?" (
    controller: 'myRedirectControlller', action: 'myRedirectAction', params:[ controller: $controller, action: $action, id: $id ]
)

"/user/list" ( controller:'user', action:'list' )

and in the action you get the values normallny in params:

log.trace 'myRedirectController.myRedirectAction: ' + params.controller + ', ' + params.action + ', ' + params.id
Tom Metz
  • 919
  • 6
  • 7
  • Thanks for the answer, but I cannot change the main mapping to a redirect controller because I have other Controllers that do follow the Grails convention of `controller/action/id`. I was hoping to be able to do explicit Redirects. – Philip Tenn Sep 27 '12 at 13:53
  • If you have only small ammount of controller with standard mapping, you can prepare additional specific rules. I have updated my comment to include one. – Tom Metz Sep 27 '12 at 14:02
  • Thanks, Tom. I accepted your answer, it led me on the right path. I actually ended up creating the `RedirectController`, but instead of mapping `/$controller/$action?/$id?" to it, I created an entry for `property/create` to it, and will do so for each URL that I do not want repeated. – Philip Tenn Sep 27 '12 at 18:53
  • Thanks a lot, but honestly I don't fully understand your final solution. You have more "patterns" mapped to the RedirectController? – Tom Metz Sep 27 '12 at 19:02
  • Good point ... I edited my original question with the implemented solution. Please see **Updated** section. – Philip Tenn Sep 27 '12 at 19:13
2

As of Grails 2.3, it is possible to do redirects directly in the UrlMappings, without the need for a redirect controller. So in case you ever upgrade, you can redirect in UrlMappings like so, as per the documentation:

"/property/create"(redirect: '/add-property-to-directory')

Request parameters that were part of the original request will be included in the redirect.

AForsberg
  • 1,074
  • 2
  • 13
  • 25
1

In recent Grails version (currently 5.x) you need to pass a Map into the redirect: property:

"/viewBooks"(redirect: [uri: '/add-property-to-directory'])

https://docs.grails.org/latest/guide/theWebLayer.html#redirectMappings

Matthijs Bierman
  • 1,720
  • 9
  • 14