SPRINGMVC jump from controller to another controller

Source: Internet
Author: User

1. Requirements Background   Requirements: Spring MVC Framework controller jumps, 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. 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. Solution   Demand There is definitely a solution to solve, one after the other, explaining that spring is a lot of jump way, I just say some self-think good, commonly used, spring-loaded some of the classes and methods.   (1) I'm in the background. 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    return new Modelandview ("Redirect:/tolist");    This way you can redirect to toList this method     way two: return string          return "redirect:/toList";   & nbsp 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 the query criteria, after jumping my query conditions can not be discarded, so that need to take parameters, with parameters can be stitching url    way one: their own manual splicing url        & nbsp New Modelandview ("Redirect:/tolist?") param1= "+value1+" ¶m2= "+value2";          There is a drawback, that is, the Chinese language may have garbled problems.     Way II: With Redirectattributes, this is found to be a more useful one of the class &nbsp         here with its AddAttribute method, this actually redirects the 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 in Tocontroller In this method, the parameter can be obtained by obtaining the parameter and then passed to the page. The URL of the past is still the same as the way.   (3) with parameter not stitching URL page can also get value (focus on this)       General I guess redirect to all want to use this way:      @RequestMapping ("/save") & nbsp Public String Save (@ModelAttribute ("form") Bean form,redirectattributes attr)             Throws Exception {    String code =  service.save (form);    if (code.equals (")") {      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: "+rsp.getcode (). GetCode () +", Error: "+rsp.getcode ( ). GetName ());      return "Redirect:/maintenance/toaddconfigcenter";   } }@ Requestmapping ("/index")       public String Save (@ModelAttribute ("form") Bean form, Redirectattributes attr)            throws Exception {      return "redirect:/ Main/list "; } page value No, I said it, 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. Summary   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.   Actually there is nothing, but know this is very simple, did not understand, now understand, and share with you. Give me a message if you have any questions.

Springmvc jump from 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.