Problems passing form feedback between controllers

2019-09-07 01:50发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

Place this on your JSP:

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