Grails UrlMapping Redirect to save DRY

I am working with Grails 2.1.1 and would like to add some customized URLs that map to controller actions.

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

For example, I created an add-property-to-directory UrlMappings 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 press /mysite/add-property-to-directory and it will execute PropertyController.create , as expected.

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

In the spirit of DRY, I would like to do 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 how I can do this in the Grail?

Many thanks!

UPDATE

Here is the solution 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) } } 
+8
grails
source share
2 answers

This can be achieved:

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

and in action you will get normallny values ​​in params:

 log.trace 'myRedirectController.myRedirectAction: ' + params.controller + ', ' + params.action + ', ' + params.id 
+3
source share

As with Grails 2.3, you can redirect directly to UrlMappings without the need for a redirection controller. Therefore, if you are ever updating, you can redirect to UrlMappings as described in the documentation :

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

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

0
source share

All Articles