Java, Spring Framework MVC - redirection

后端 未结 3 718
感情败类
感情败类 2021-01-26 21:57

I am using spring framework 3. I have a form for posting comments to the article. When the form is submitted, it is checked if there any errors. In case there is no errors, cont

3条回答
  •  灰色年华
    2021-01-26 22:28

    The display of errors in works like this:

    • During validation/binding, the Errors are saved as an attribute in the HttpServletResponse object
    • The implementation of this JSP tag calls response.getAttribute(name) to find the Errors instance to display

    When you use redirect:url, you are instructing Spring to send a 302 Found to the client's browser to force the browser to make a second request, to the new URL.

    Since the redirected-to page is operating with a different set of request/response objects, the original Errors is lost.

    The simplest way to pass "errors" to a page you want to redirect to in order for the new page to display it would be to handle it yourself, by adding a message to the Session object which the second page's controller can look at (or by passing an argument in the URL).

提交回复
热议问题