Hi friends I am back with a new blog is how to change a default pattern of controller names in URL.
Let us consider case 1 :
We have a controller by name of AdminController .
I am looking to create a new alias “/reports” in URL for AdminController all actions.
To achieve this follow the below steps :
Step 1 :
Go to UrlMappings.groovy and paste below code :
static mappings = { "/reports/$action?/$id?" { controller = 'admin' namespace = 'reports' } "/$controller/$action?/$id?" { constraints { // apply constraints here } } }
Step 2 :
Go to AdminController.groovy and paste below code :
class AdminController { static namespace = "reports"
Now test your AdminController by passing in URL “/reports” behalf of “/admin”.
Let us consider case 2 :
I have 2 controllers in 2 different packages by same name as AdminController.
I am looking to provide them different aliases to call.
For 1st controller “/reports” and for 2nd “/users”.
To achieve this follow the below steps :
Step 1 :
Go to UrlMappings.groovy and paste below code :
static mappings = { "/reports/$action?/$id?" { controller = 'admin' namespace = 'reports' } "/users/$action?/$id?" { controller = 'admin' namespace = 'users' } "/$controller/$action?/$id?" { constraints { // apply constraints here } } }
Step 2 :
Go to AdminController.groovy and paste below code (Of 1st package):
package com.pack1; class AdminController { static namespace = "reports"
Step 3 :
Go to AdminController.groovy and paste below code (Of 2nd package):
package com.pack2; class AdminController { static namespace = "users"
Now test your AdminController by passing in URL “/reports” and “/users”.
Recent Comments