Springmvc how to redirect, jump from one controller to another controller

Source: Internet
Author: User

In the first case, jump without parameters:

Method One: Use Modelandview

return new Modelandview ("Redirect:/tolist");

This way you can redirect to ToList.

Method Two: After return directly, redirect plus the address to jump, that is, you can jump from the first controller to a second controller, such as the code in the method one

Method Three: See the Blue box, as long as the return directly after the controller to jump to the method name can be, note that this method name is not the path of requestmapping, is the controller of the specific method,

Slices of 3 and 4, after walking 3, he will find 4 instead of 2 (2 is the path mapped in requestmapping), this is like a Java method of overloading, such as the code in the method two

Second case, with parameter jump

Method One: Use directly in the back? Stitching.

Method Two: With Redirectattributes, this is found to be a more useful one class here with its AddAttribute method, this actually redirects past after you look at the URL, is it automatically spell your URL for you.

Method of 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.

Method Three: With parameters do not splice URL page can also get the value (the focus is this)

@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"; }//page values, directly with the EL expression can be obtained, the principle here is placed in the session, the session will jump to the page immediately after removing the object. So when you refresh, the value will be lost. //3. The bottom or two kinds of jumps, just spring and then the package, so there are many different ways to jump, you can also seal one, you can use the most primitive response, there is no problem. Okay,//that's it. In fact, there is nothing, but know this is very simple, did not understand before, now understand, and everyone to share. Give me a message if you have any questions. 

Springmvc how to redirect, jump from one controller to another controller

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.