SPRINGMVC jump from controller to another controller

Source: Internet
Author: User

http://blog.csdn.net/jackpk/article/details/44117603

"PK Pro Test"

The normal jump can be written as follows:

return "Forward:aaaa/bbbb.do";

return "Redirect:aaaa/bbbb.do";

return new Modelandview ("forward:bbbb.do", null);

return new Modelandview ("redirect:bbbb.do", null);

The Web-inf/web.xml is configured as follows

<servlet>        <Servlet-name>Spring-dispatcherservlet</Servlet-name>        <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>        <Init-param>            <Param-name>Contextconfiglocation</Param-name>            <Param-value>Classpath:mescrow-appcontext.xml</Param-value>        </Init-param>        <Load-on-startup>1</Load-on-startup>    </servlet>    <servlet-mapping>        <Servlet-name>Spring-dispatcherservlet</Servlet-name>        <Url-pattern>*.do</Url-pattern>    </servlet-mapping>

SPRINGMVC jump from controller to another controller
Refurl:http://blog.csdn.net/liupeng_family/article/details/38229125?utm_source=tuicool

1. Requirements background
Requirement: The spring MVC Framework controller jumps between the controllers and needs to be redirected. 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. Workaround
There is definitely a solution to the demand, a solution, explain the spring of the 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 way you can redirect to ToList.
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 you 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:

Attr.addattribute ("param"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)
In general, I estimate redirection to all the ways that you want to do 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 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 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. All right, here we go.
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.

Forward and redirect in Spring MVC

http://blog.csdn.net/zzjjiandan/article/details/19491253

In Spring MVC, when we return to the logical view, the framework parses the specific view with viewresolver and renders it to the browser. Assuming that the logical view is named Hello, through configuration, we configure a viewresolver as follows:

<Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">      <Description>If the logic tries to name "Hello", Viewresolver will parse into/web-inf/jsp/hello.jsp</Description>      < Propertyname= "Order"value= "Ten" />      < Propertyname= "prefix"value= "/web-inf/jsp/" />      < Propertyname= "suffix"value= ". jsp" />  </Bean>  

In fact, the framework is forwarded to the/web-inf/jsp/hello.jsp by means of forward. If the logical view name is/hello, it is actually forwarded to/web-inf/jsp/hello.jsp, that is,/web-inf/jsp//hello.jsp is equivalent to/web-inf/jsp/hello.jsp.

Now there is a problem, if/hello is a controller mapping, I want to forward to this controller, how to do? We can use the forward prefix to achieve the purpose of forwarding to other resources:

 Public String handle () {      //  return "Forward:/hello" = + forwards to controller       capable of matching/hello  The return "Hello" and "=" is actually forwarded, except that the framework will find the view corresponding to the      logical views and render //  return "/hello" = Return "Hello"      return "Forward:/hello";  }  

Similarly, if we want to redirect to a resource, we can achieve the purpose of redirecting to other resources by redirect the prefix:

 Public String handle () {      // redirect to/hello resource      return ' Redirect:/hello ';  }  

SPRINGMVC jump from controller to another controller (GO)

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.