Grails UrlMapping Redirect to keep DRY - grails

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)
}
}

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

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.

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

Related

Grails 4.1 - Static Mapping in URL Mappings not working properly after upgrade

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?
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.

Does Grails supports Restful nested URLs?

Does Grails supports Restful nested URLs like '/articles/1/comments/5' by default? If not is there any plugin for that?
See here. Nested RESTful URLs will be supported in Grails 2.3.
If you're bound to an older release of Grails (e.g < 2.3), and the available plugins don't work out, you can use named URL mappings to produce an effective restful mapping.
Here's an example from a project of mine - I've left out some details, but hopefully this gets you started if you decide to give this approach a try.
In your UrlMappings.groovy
/** 1. Mappings can handle multiple actions depending on HTTP
method like Rest. Names are a little clunky, like this would
be more appropriate as "resource" vs "showResource" but we didn't want
potential naming conflict in future release
2. TODO: DRY constraints - make constraints global
3. make sure controllers have proper actions defined
*/
/** RESTFUL mapping for single resource */
name listResources: "/$controller" {
action = [GET: "list", POST: "save"]
}
name createResource: "/$controller/create" {
action = [GET: "create" ]
}
name deleteResource: "/$controller/$id?/delete" {
action = [POST: "delete", DELETE: "delete"]
constraints { id(matches: /[0-9]+/) }
}
name editResource: "/$controller/$id?/edit" {
action = [GET: "edit", PUT: "update", POST: "update"]
constraints { id(matches: /[0-9]+/) }
}
name showResource: "/$controller/$id?" {
action = [GET: "show", PUT: "update", POST: "update", DELETE: "delete"]
constraints { id(matches: /[0-9]+/) }
}
/** RESTFUL mapping for CHILD with PARENT */
name listChildResources: "/$parentResource/$pid/$controller" {
action = [GET: "list", POST: "save"]
constraints { pid(matches: /[0-9]+/) }
}
name createChildResource: "/$parentResource/$pid/$controller/create" {
action = [GET: "create" ]
constraints { pid(matches: /[0-9]+/) }
}
name showChildResource: "/$parentResource/$pid/$controller/$id?" {
action = [GET: "show", PUT: "update", POST: "update", DELETE: "delete"]
constraints {
id(matches: /[0-9]+/)
pid(matches: /[0-9]+/)
}
}
name editChildResource: "/$parentResource/$pid/$controller/$id?/edit" {
action = [GET: "edit"]
constraints {
id(matches: /[0-9]+/)
pid(matches: /[0-9]+/)
}
}
Make sure you controllers have actions and supported HTTP methods define, eg
static allowedMethods = [
save: "POST",
update: ["POST", "PUT"],
delete: ["POST", "DELETE"]
]
Then use the mappings like so (for example lets say we have Gardens and Plants as resources).
//show a garden
<g:link mapping="showResource" controller="garden"
id="${gardenInstance.id}">${gardenInstance.name}</g:link>
//create a plant for garden
<g:link mapping="createChildResource" controller="plant"
params="[parentResource: 'garden', pid: gardenInstance.id]">Add Plant</g:link>
//show list of plants within a garden
<g:link mapping="listChildResources" controller="plant"
params="[parentResource: 'garden', pid: gardenInstance.id]">List plants for Garden</g:link>
Shown here it's pretty verbose, but you could put all of this into TagLib and have something like.
<g:restShow resource="garden"
id="${gardenInstance.id}">${gardenInstance.name}</g:restShow>
<g:restCreate" resource="plant"
parent="${gardenInstance}">Add Plant</g:restCreate>

How to exclude specific keywords from UrlMapping in Grails?

I use the following url mapping in Grails:
"/$id"{
controller = "user"
action = "show"
}
to map urls like mydomain.com/someusername
How do I attach constrains to the url mapping to exclude keywords like "login", "logout",...
I.e.,
mydomain.com/someusername should route to mydomain.com/user/show/someusername,
mydomain.com/login should not route to mydomain.com/user/show/login.
You can use contrainsts for this mapping:
"/$id"{
controller = "user"
action = "show"
constraints {
//add a validator for $id from url mapping
id(validator: {
return !(it in ['login', 'logout'])
})
}
}
Use a filter, and redirect from it.
class UrlMappingFilters {
def filters = {
filterUrlKeywords(controller: '*', action: '*') {
def keywords = ['login', 'logout']
before = {
if (params.id in keywords) {
// redirect to start, or render error...
redirect(uri: '/')
return false
}
}
}
}
}
If you want to make it specific to a controller or action, use its name instead of the '*'.
Read more:
Official docs on Grails filters
Reference
I have done this type of thing for REST based endpoints numerous times, and Grails is smart enough to figure out what you want. Note that ordering in the UrlMappings file may be important.
For example, you can define this:
class UrlMappings {
static mappings = {
"/login" { controller: 'auth', action: 'login' }
"/logout" { controller: 'auth', action: 'logout' }
"/$id" { controller: 'user', action: 'view' }
}
}
Then when you hit "/login" you will go in the auth:login method, but when you hit "/userid" you will be sent to user:view:userid as expected.

Grails: Can't figure out why I'm getting a 404

I'm using Grails 1.3.6. I have this file ...
grails-app/views/home/design/index.gsp
Here is what is defined in my HomeController. Sadly, whenever I visit, "http://localhost:port/context-path/design/", I get a 404 error. The server starts normally and there are no errors in the logs. What can I do to get my page instead of the 404?
def index = {
def folder = params.folder;
def page = params.page;
if (page) {
try {
def contents = IOService.getFileContents(folder, page)
response.setContentType("application/json")
response << contents
} catch (FileNotFoundException e) {
response.status = 404;
} // try
} else {
render(view: "/home/${folder}/index")
} // if
}
My URLMappings file consists of ...
static mappings = {
"/$folder?/$page"{
controller = "home"
action = "index"
}
"/"(view:"/index")
"500"(view:'/error')
}
Thanks, - Dave
If you want to be able to access
/context-path/home/design
Your action needs to be named design, i.e.
class HomeController {
def design = {
}
}
The Grails convention is always /context-path/controllerName/actionName (unless you have it mapped differently in grails-app/conf/URLMappings.groovy).
Your example's a bit unclear which path you're trying to access. To address both:
If you want /context-path/design, you need a DesignController with an index action (because if no action is supplied in the URL, Grails looks for the index action).
If you want /context-path/home/design, you need a HomeController with a design action.
Edit:
In the comments, you express the want to be able to have /context-path/design map to the HomeController index action. You can do this with grails-app/conf/URLMappings.groovy:
"/design"(controller: 'home', action: 'index')
Since it seems you have two distinct actions, I would set things up a bit differently:
def indexWithPage = {
def folder = params.folder;
def page = params.page;
try {
def contents = IOService.getFileContents(folder, page)
response.setContentType("application/json")
response << contents
} catch (FileNotFoundException e) {
e.printStackTrace();
response.status = 404;
} // try
}
def index
def folder = params.folder;
render(view: "/home/${folder}/index")
}
with a URLMaping of:
static mappings = {
"/$folder/$page"{
controller = "home"
action = "indexWithPage"
}
"/$folder"{
controller = "home"
action = "index"
}
"/"(view:"/index")
"500"(view:'/error')
}
I also threw a e.printStackTrace(); in there to help us determine whether you're getting YOUR 404 or the action is truely not being called.

Best practices for grails index page

What is the right way to populate the model for the index page in a grails app? There is no IndexController by default, is there some other mechanism for getting lists of this and that into the model?
I won't claim that this is the right way, but it is one way to start things off. It doesn't take much to have a controller be the default. Add a mapping to UrlMappings.groovy:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"500"(view:'/error')
"/"
{
controller = "quote"
}
}
}
Then add an index action to the now default controller:
class QuoteController {
def index = {
...
}
}
If what you want to load is already part of another action simply redirect:
def index = {
redirect(action: random)
}
Or to really get some reuse going, put the logic in a service:
class QuoteController {
def quoteService
def index = {
redirect(action: random)
}
def random = {
def randomQuote = quoteService.getRandomQuote()
[ quote : randomQuote ]
}
}
I couldn't get Ed T's example above to work. Perhaps Grails has changed since then?
After some experimentation and some rummaging on the net, I ended up with this in UrlMappings.groovy:
"/"(controller: 'home', action: 'index')
My HomeController looks like this:
class HomeController {
def index = {
def quotes = = latest(Quote.list(), 5)
["quotes": quotes, "totalQuotes": Quote.count()]
}
}
And in views/home, I have an index.gsp file. That makes the index.gsp file in views unnecessary, so I removed it.
The good answer: If you need to populate a model for the index page, it's time to change from using a straight index.gsp to an index controller.
The evil answer: If you create a filter whose controller is '*', it'll get executed even for static pages.
In grails 1.3.6 for just adding
"/index.gsp"(uri:"/")
to UrlMappings.groovy worked fine for me. It has the same effect as adding a new controller and mappings like described before.
Below is my complete UrlMappings.groovy:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
"/index.gsp"(uri:"/")
}
}

Resources