Grails 2.3.0 Redirects In URL Mappings

    Tuesday, September 24th, 2013     Grails,Grails 2.3.0    

Prior to Grails 2.3.0 redirects were not supported in URL Mappings and the available solutions were not DRY. There have been various discussions on stackoverflow, Mailing List , Jira and on the blogs to add redirect in URLMapping.groovy.

Grails team has finally added this functionality in 2.3.0 release. It is very easy to manage redirects in URL mappings now and this solution is more DRY.

Quick Code :

"/viewUsers"(redirect: '/user/list')	// It will accept a string argument

"/viewUsers"(redirect: [controller: 'user', action: 'list', permanent: true])	
// It will also accept a normal map accepted by redirect method

Note : "permanent: true" will redirect with HTTP 301 status code which means moved permanently otherwise HTTP 302 status code will be used. 

Now an easy way to answer this stackoverflow question is to add another mapping in URLMapping as follows :

class UrlMappings {

static mappings = {
    "/add-property-to-directory"(controller: "property", action: "create")
    "/property/create"(redirect:"/add-property-to-directory")   //This will do the job and issue 301 redirect
        constraints {
            // apply constraints here