Spring MVC redirection and forwarding

Source: Internet
Author: User

Technology Exchange Group: 233513714

Forwarding and redirection

When starting Java EE, the two concepts of forwarding (forward) and redirection (redirect) may not be clear. This article first through the code instance and the running result picture perceptual knowledge two difference, then gives the two definition.

1. General usage, return a view

@RequestMapping (Value= "/testa", Method=requestmethod.get) public String inputdata () {return Span class= "hljs-string" > "Testa"; //spring frame find corresponding view and render} @RequestMapping (value= "/testa", Method=requestmethod.post) public string outputdata (HttpServletRequest request) {String UserName = request.getparameter ( "name"); String password = request.getparameter ( "pwd"); Request.setattribute (  "name", UserName); Request.setattribute ( "pwd", password); return  "TESTB"; //spring frame find corresponding view and render}           

Open the Testa Web page:

Enter user name: Spring, Password: spring:

Click the login button and the page becomes as follows:

Refresh again, Google Browser prompts to resubmit the form.

Compare the image and find that the URL in the browser's input box is the same, but different scenarios show different view. The model is shared when jumping (the form is submitted repeatedly).

2. Forwarding (forward)
@RequestMapping (Value="/testa", Method=requestmethod.get)Public StringInputdata () {Return"Testa";The spring framework finds the corresponding view and renders} @RequestMapping (Value= "/testa", Method=requestmethod.post) public string outputdata (HttpServletRequest request) {string UserName = Request.getparameter ( "name"); String password = request.getparameter ( "pwd"); Request.setattribute (  "name", UserName); Request.setattribute ( "pwd", password); //forward to/testb controller method (i.e. Outputdatax) return  "FORWARD:/TESTB"; } @RequestMapping (value= "/testb", Method=requestmethod.post ) public String outputdatax (HttpServletRequest request) {return  "Testb";}         

Open the Testa Web page:

Enter user name: Spring, Password: spring:

Click the login button and the page becomes as follows:

Debug analysis: Forward followed by a resource. When the program runs to return "Forward:/testb", it executes the method outputdatax that the resource corresponds to.
In addition, the URL of the browser does not change when forwarding.

Refresh again, Google Browser prompts to resubmit the form.

3. Redirect (redirect)
@RequestMapping (Value="/testa", Method=requestmethod.get)Public StringInputdata () {Return"Testa";The spring framework finds the corresponding view and renders} @RequestMapping (Value="/testa", Method=requestmethod.post)Public StringOutputdata (HttpServletRequest request) {String UserName = Request.getparameter ("Name"); String Password = request.getparameter ("PWD"); Request.setattribute ("name", UserName); Request.setattribute ("pwd", password); //Redirect to/testb Controller method (i.e. Outputdatay) on return "Redirect:/testb";} @RequestMapping (value="/testb", method=requestmethod.post) publicString Outputdatax ( HttpServletRequest request) { return "Testb";} @RequestMapping (value="/testb", method=requestmethod.get) publicString Outputdatay ( HttpServletRequest request) { return "Testb";}           

Open the Testa Web page:

Enter user name: Spring, Password: spring:

Click the login button and the page becomes as follows:

Debug Analysis: redirect followed by a resource. When executed to return "REDIRECT:/TESTB", the resource corresponding to the method Outputdatay is executed. The page has no data displayed because the redirect model is not shared.
In addition, the URL in the browser's input box also changes after redirection.

After refresh, Google Chrome does not prompt to resubmit the form

Summary:
Often said can be redirect:url to prevent repeated submission of the form, that is, the meaning of the above process.
The principle is that for redirect, the attribute of the request will not be passed on to the session, and the session is removed immediately after jumping to the page. So when you refresh, the value will be lost.

If you want the attribute of the request to be passed, you can use the Redirectattributes class.

@RequestMapping (Value="/testa", Method=requestmethod.get)Public StringInputdata () {Return"Testa";The spring framework finds the corresponding view and renders} @RequestMapping (Value="/testa", Method=requestmethod.post)Public StringOutputdata (httpservletrequest request, Redirectattributes redirectattributes) {String userName = Request.getparameter ("Name"); String Password = request.getparameter ("PWD"); Request.setattribute ("Name", UserName); Request.setattribute ("pwd", password);Redirect to/testb on the Controller method (i.e. Outputdatay)The two methods of redirecting parameters are Redirectattributes.addattribute ("Name", UserName); Redirectattributes.addflashattribute ("pwd", password); return "Redirect:/testb",} @RequestMapping (value="/testb", Method=requestmethod.post) public String Outputdatax (httpservletrequest request) { return "Testb";} @RequestMapping (value="/testb ", method=requestmethod.get) public String Outputdatay (HttpServletRequest request) {string userName = Request.getparameter ("name"); Request.setattribute ("name", UserName); return "Testb";}                

Run as follows:

The above example provides two methods for passing parameters using Redirectattributes:
1. Passing parameters using the AddAttribute method of the Redirectattributes class is followed by the URL, as shown in Google Chrome, and the URL is http://localhost:8080/testb?name=spring
2. Passing parameters using the Addflashattribute method of the Redirectattributes class will not follow the URL and will temporarily save the parameter value to the session, which is removed from the session after the redirect URL gets the parameter. The redirect here must be a method mapping path, and the JSP is invalid. You will find that the PWD will only appear once in the redirect JSP page, and the PWD will not appear again after the refresh. As a result of the refresh, the password pwd display is empty. This verifies that the PWD is removed from the session after being accessed. You can use this method for preventing duplicate submissions.

Defined

As an experienced servlet/jsp programmer, you must know the difference between forwarding and redirection. The forward weighting is fast because redirects go through the customer service side, and forwarding is not. However, it is better to use redirection, and if you need to redirect to an external Web site, forwarding is not available.

Personal understanding: Like server window A order meal, window B take meal. Forwarding is the browser to the server window a point meal, after ordering the meal, the server inside from window B to take meals, the food directly to the browser. Redirection is the browser to the server window a meal, the server does not help the browser from the window B to take the meal, but to inform the browser to B to take the meal. The browser receives the information and then sends a request for a meal to window B.

Spring MVC redirection and forwarding

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.