Springmvc--redirect redirect Jump pass value

Source: Internet
Author: User

Spring MVC framework controller jumps between controllers and requires redirection. There are several situations: without parameter jump, with the parameter splicing URL form jump, with parameters do not join parameters jump, the page can also be displayed.
First, let's introduce a redirect without parameters:
I'm backstage. A controller jumps to another controller, so why is there such a requirement? I have a list page, and then I will do a new operation, added in the background after the completion of I want to jump to the list page, do not need to pass parameters, List page default query all.
Mode one: Use Modelandview (This is the method used by Spring 2.0)
return new Modelandview ("Redirect:/tolist");
This way you can redirect to ToList.
Mode two: return string
Return "redirect:/toList";
And then we're talking about redirection with parameters.
In the second case, the list page has query criteria, after jumping my query conditions can not be discarded, so that need to take parameters, with parameters can be spliced URL

Way one: Manually stitching the URL yourself
New Modelandview ("Redirect:/tolist?") param1= "+value1+" &param2= "+value2);
There is a drawback, that is, the Chinese language may have garbled problems.

Way two: With Redirectattributes, this is found to be a more useful class
Here with its AddAttribute method, this actually redirects past after you look at the URL, is it automatically spell your URL for you.
How to use:
Public String test (redirectattributes attributes)
{
Attributes.addattribute ("Test", "Hello");
return "Redirect:/test/test2";
}
This parameter can be obtained by obtaining parameters in the Tocontroller method, which is then passed to the page. The URL of the past is still the same as the way. If you carefully value the URL after the orientation, you will find that the above address is the same, so that the above method will appear the problem.
The point is, let's introduce a no Chinese garbled, and will not appear on your URL to the data you want to pass, so that you can ensure the security of data transmission
Public String Red (redirectattributes attributes)
{
Attributes.addflashattribute ("Test", "Hello");
return "Redirect:/test/test2";
}
We use the above method for data transmission you will find that the URL will not appear on the data you want to pass, then where the data go, we will see this is Spring 3.0 new features, Attributes.addflashattribute ("Test", " Hello ") The actual stored properties are Flashmap, so what is Flashmap?
Flash Properties and Redirectattribute: The output of a request is stored through FLASHMAP, and as input to the request when another request is entered, a typical scenario such as redirection (Post-redirect-get mode, 1, Post the next time you need to put the data in Flashmap;2, redirect, 3, through get access to redirect the address, at this time Flashmap will put 1 to flashmap the data out and put into the request, and delete from Flashmap , which allows data to be saved between two requests and prevents duplicate form submissions.
Spring Web MVC provides flashmapmanager for managing Flashmap, using Sessionflashmapmanager by default, where data is stored by default in session
Now that we know what is going on, then we can extract it, how to extract it, many people will say, since there is a session, that is obtained from the session, the results found no, how to do?
I'll give you two ways to extract the data from the Addflashattribute.
Method One: Using HttpServletRequest
Public String test2 (HttpServletRequest request)
{
map<string,?> map = requestcontextutils.getinputflashmap (request);
System.out.println (Map.get ("test"). ToString ());
return "/test/hello";
}
Method Two: Take advantage of the label provided by spring @modelattribute
public string Test2 (@ModelAttribute ("test") string str)
{
System.out.println (str);
return "/test/hello";
}
The above two methods are in the background controller layer to get the value of the method, if it is in the foreground page, it will be relatively simple, directly using the EL Expression can be taken to the data

Springmvc--redirect redirect Jump pass value

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.