Spring MVC controller Jump redirect pass-through parameter

Source: Internet
Author: User

Go from:Spring MVC controller jumps redirect to parameter (go)

1. Requirements Background requirements: Spring MVC Framework controller jumps between, need to redirect. 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. I thought it was a simple thing, and personally think that the more commonly used one way, 100 degrees all have, these are not problems, but 100 degrees unexpectedly to my surprise, a bunch of not I want results. Helpless Ah, write a comparison of their own all for the later 100 degrees, ha haha ... It's not all that's written. People have given me the impetus to write this blog. 2. The solution needs to have a definite solution, one after the other, explaining that spring's jump way a lot of, I just say some self-think good, common, spring-loaded some classes and methods. (1) I have a controller in the background to jump to another controller, why there is such a requirement, is the case.        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
return New Modelandview ("Redirect:/tolist");

This allows redirection to the ToList method.

Mode two: return string
Return
Other ways: There are many other ways, there is no introduction, such as response and so on. This is a redirect without parameters. (2) The second situation, 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: their own manual splicing URL
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 a relatively useful class to find 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:
Attr.addattribute ("param", value); return "Redirect:/namespace/tocontroller";
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. (3) with parameters do not splice URL page can also get the value (the focus is this) generally I guess redirect to all want to use this way:
@RequestMapping ("/save")    PublicString Save (@ModelAttribute ("form") Bean form,redirectattributes attr)throwsException {String code=service.save (form); if(Code.equals ("000") {Attr.addflashattribute ("Name", Form.getname ()); Attr.addflashattribute ("Success", "Add success!"); return"Redirect:/index"; }Else{Attr.addattribute ("ProjectName", Form.getprojectname ()); Attr.addattribute ("Enviroment", Form.getenviroment ()); Attr.addflashattribute ("MSG", "Add Error!" error code is: "+rsp.getcode (). GetCode () +", Error: "+Rsp.getcode (). GetName ()); return"Redirect:/maintenance/toaddconfigcenter"; }} @RequestMapping ("/index")     PublicString Save (@ModelAttribute ("form") Bean form,redirectattributes attr)throwsException {return"Redirect:/main/list"; }

The principle here is to place the session, and the session will remove the object immediately after jumping to the page. So when you refresh, the value will be lost.

3. Summarize the bottom or two kinds of jumps, just spring has been encapsulated, so there are many different ways to jump, you can also seal one, you can use the most primitive response, there is no problem. All right, here we go.

Spring MVC controller Jump redirect pass-through parameter

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.