Spring MVC redirect/forward Form repeating submission

Source: Internet
Author: User
Tags commit

I've been using spring MVC lately, and it's not very easy to redirect forward and commit, just sort it out.


Spring MVC finally return a string, typically a page path below webcontent, such as:

return "/web-inf/view/login.jsp";

The above sentence means that after a request is processed, the login.jsp page is displayed and the page is rendered with data from a bunch of objects, such as request, to populate the dynamic data.


And if you say return the address of an action. After my test, I found that there are situations that can be forwarded, which, like forward, pass parameters to the new action. But some do not, will error, said the return URL is not a path, that is, improper it is an action. This is not important, because it is not written at all.


What if you really want to return an action? For example, a action jumps to B action instead of xxx.jsp, in two ways:

1.forward:

return "forward:/manage/login_check.htm";

In this way, the parameters set in A are also available in B, and the address bar is the address of a action.

Here are the parameters that can be obtained, divided into two types, one is submitted by the form, that is, a in the method parameter is obtained, then B also in the action method's parameter list to write the same parameters, then you can get. The parameters within the request are only visible in the action's method. Another is a in the response inside, can also get, of course, only in the foreground with El display.

2.redirect:

return "redirect:/manage/login_check.htm";

In this way, the parameters set in a are not available in B, and the address bar becomes the address of the B action.

At that time, the response of a and the request were not.


After figuring it out, then what does this have to do with form submissions?

For example, we have a a.jsp page with a form form, a list, and then a action to handle it, and when we click Submit, the action adds a piece of data to the list and returns to the A.jsp page. Then we will definitely write this at the end of a action:

return "/view/a.jsp";
When returned to the a.jsp, the browser will cache the previously submitted data, we refresh the page, even if no form, the browser will also put the previous form of the cache, submitted to the background, causing the background and insert a piece of data.

The solution is to use Redirect to an action, which clears out all parameters and does not repeat the commit.


So what exactly is the reason for repeating the submission?

The first thing you have to figure out is: What address does the browser request when we refresh again? When a form is submitted to an action for the first time, the action returns a JSP page, but it is important that the page is a display of results, the address of the browser address bar, or the address of the action. The address used in Javaweb is always an action address. If we refresh at this point, instead of refreshing the result page of that JSP, but having visited the action once, the action will be processed again, which is equivalent to repeating the commit.

If the use of a get pass, then the effect is more intuitive, when the action returned to the JSP results view, we can see the browser address bar is the address of the action, and then the interface also stitching the parameters passed, this time click Refresh, the result is to use those parameters to request action, of course, will repeat. If you want to dig deep, then you have to figure out the browser's processing mechanism, why the data is cached ... No time.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.