简体   繁体   English

在控制器之间传递表单反馈以重新显示表单错误消息时出现问题

[英]Problems passing form feedback between controllers to re-display a form error message

Controller A shows some data and displays a form. 控制器A显示一些数据并显示一个表格。

The form submits to Controller B. 表格提交给控制器B。

When form errors are found in Controller B, the form needs to be re-displayed by Controller A. 当在控制器B中发现表格错误时,该表格需要由控制器A重新显示。

To do this requires a return redirect "blah" in Controller A. 为此,需要控制器A中的return redirect "blah"

To pass the errors back to Controller A using a redirect, I can set the error message in the model: 为了使用重定向将错误传递回控制器A,我可以在模型中设置错误消息:

model.put("errormsg", "look what happened");

This has the annoying effect of putting the entire text of the error message into the URL: 这具有将错误消息的整个文本放入URL的烦人效果:

/controllera/somemethod?errormsg=look+what+happened

However, even though I see the text on the URL, when I try to display it from the JSTL, nothing is shown: 但是,即使我在URL上看到了文本,当我尝试从JSTL中显示该文本时,也没有显示任何内容:

<c:out value="${errormsg}"/>

Is there a better approach to all of this? 是否有更好的方法来解决所有这些问题?

I'd recommend: 我建议:

  1. Modify Controller A to accept the HttpServletRequest. 修改控制器A以接受HttpServletRequest。
  2. Put the error message in the request as an attribute: req.setAttribute("errormsg", "look what happened"); 将错误消息作为属性放入请求中: req.setAttribute("errormsg", "look what happened");

The JSTL output should work just fine then. 然后,JSTL输出应该可以正常工作。

将其放在您的JSP上:

<c:out value="${param.errormsg}" />

Is there a better approach to all of this? 是否有更好的方法来解决所有这些问题?

IMHO better approach is to use standard scheme: controller A shows form, data submitted to controller A and if error occurs controller A display errors. 恕我直言,更好的方法是使用标准方案:控制器A显示表单,将数据提交给控制器A,如果发生错误,则控制器A显示错误。 If data valid controller A do redirect to controller B. 如果数据有效,则控制器A确实重定向到控制器B。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM