Music, software, life… and stuff.
[ Twitter ] [ GitHub ] [ Linked In ]
I recently hit the case where I need to forward a request in Grails to a second action, but that action needed access to the model generated by the first (note that this is different to params). The Grails documentation doesn’t mention the ability to pass models when forwarding at all, but if you check the source code you can see that the capability is there.
What’s also missing is how to access the existing model in a forwarded to or chained to action. If you trace the code, you see that the model eventually gets passed to Spring’s [WebUtils.exposeRequestAttributes()](http://static.springsource.org/spring/docs/current/javadoc-api/org/springframework/web/util/WebUtils.html#exposeRequestAttributes(javax.servlet.ServletRequest, java.util.Map)) method, that simply makes each item in the model a request attribute. So in Grails you can access them via the request variable…
class MyController {
def first = {
forward(action: 'second', model: [thing: "thing"])
}
def second = {
assert request.thing == "thing"
// go forth and do stuff
}
}
Params can only be strings where as you can pass any type via the model.
Posted: May 24th, 2010 @ 2:35 pm