通过重定向呈现命令验证错误
我无法从我的命令对象中呈现错误。它做得很好,但我的 .gsp 视图没有呈现我提出的错误。
这是我的控制器操作:
def handleModifyProfile2 = { CreditProviderModificationCommand cpmc -> // bind params to the command object
if (cpmc.hasErrors()) {
flash.message = "Error modifying your profile:"
redirect(action: "modifyProfile", params: [creditProvider : cpmc])
} ...
这是我尝试在 .gsp 视图中呈现错误的方法:
<g:hasErrors bean="${creditProvider}">
<div class="errors">
<g:renderErrors bean="${creditProvider}" as="list" />
</div>
</g:hasErrors>
如何让错误显示在视图中?
I cannot render the errors from my command object. It does the job well but my .gsp view does not render the errors I raise.
Here is my controller action:
def handleModifyProfile2 = { CreditProviderModificationCommand cpmc -> // bind params to the command object
if (cpmc.hasErrors()) {
flash.message = "Error modifying your profile:"
redirect(action: "modifyProfile", params: [creditProvider : cpmc])
} ...
Here is how I try to render the errors in my .gsp view:
<g:hasErrors bean="${creditProvider}">
<div class="errors">
<g:renderErrors bean="${creditProvider}" as="list" />
</div>
</g:hasErrors>
How can I get the errors to be displayed in the view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法使用
params
在重定向中发送命令。您有几个选择:render()
而不是redirect()
ing:这是您正在执行的操作中最常见的习惯用法。
将命令存储在会话中以在重定向中保留它:
这个选项有点值得商榷。如果操作不正确,您可能会污染会话并让对象闲置,从而占用内存。不过,如果操作正确,这可能是实现重定向后获取的一种不错的方法。
You can't send the command across in a redirect using
params
. You have a couple options:render()
in the error condition instead ofredirect()
ing:This is the most common idiom for what you're doing.
Store the command in the session to persist it across the redirect:
This option is somewhat questionable. If not done correctly, you can pollute the session and leave objects hanging around, taking up memory. If done correctly, though, it can be a decent way to implement a post-redirect-get.
对于 Grails 3(我不知道这是否之前有效),可以使用 为此使用闪光灯。根据文档,闪存将“在下一个请求结束时被清除”。
我喜欢使用这样的模式:
我不喜欢使用 render() 来进行这种错误处理,因为它会使浏览器中显示的 URL 与显示的页面不一致。例如,当用户设置书签时,这种情况就会中断。
With Grails 3 (I don't know if this worked earlier) it's possible to use the flash for this. According to the documentation, the flash will be "cleared at the end of the next request".
I like to use a pattern like this:
I don't like to use
render()
for this kind of error handling, because it makes URLs shown in the browser inconsistent with the shown page. This breaks when users set bookmarks, for example.