Grails informational messages
My Grails app displays informational messages to the user after a request has been processed. These are typically either success messages:
User "John@example.com" successfully created
Or error messages:
Please fix the validation errors shown below
If I do a redirect after processing the request, then I store the messa开发者_StackOverflow社区ges in the flash scope. But if I call render
after processing the request, I can't store the messages in the flash scope, because the message will then exist in the flash scope for one request too many, so I'm not sure what is the best way to pass these messages from the controller to the view?
An obvious option is to just pass them in the model along with the other data, but I'd prefer to keep these messages separate from the "real" view model, are there any alternatives?
In the case of the example shown above, where I want to show an error message associated with a command object, I considered calling Errors.reject(msgCode) passing it the code for the "Please fix the...." message, is this an abuse of the global errors concept?
Have you considered using the session object? When I have infoermation that needs to live for multiple requests but does not to be persisted I usually store it in the session object as an objecct. This can easily be accessed from controlers or views.
I think this problem is covered by the one time data plugin http://www.grails.org/plugin/one-time-data
Have you tried putting your data in the request object?
精彩评论