Problems passing form feedback between controllers

2019-09-07 01:37发布

Controller A shows some data and displays a form.

The form submits to Controller B.

When form errors are found in Controller B, the form needs to be re-displayed by Controller A.

To do this requires a return redirect "blah" in Controller A.

To pass the errors back to Controller A using a redirect, I can set the error message in the model:

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

This has the annoying effect of putting the entire text of the error message into the 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:

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

Is there a better approach to all of this?

3条回答
冷血范
2楼-- · 2019-09-07 01:59

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. If data valid controller A do redirect to controller B.

查看更多
Emotional °昔
3楼-- · 2019-09-07 02:00

I'd recommend:

  1. Modify Controller A to accept the HttpServletRequest.
  2. Put the error message in the request as an attribute: req.setAttribute("errormsg", "look what happened");

The JSTL output should work just fine then.

查看更多
Emotional °昔
4楼-- · 2019-09-07 02:03

Place this on your JSP:

<c:out value="${param.errormsg}" />
查看更多
登录 后发表回答