Spring forwards forward and redirect redirect

Source: Internet
Author: User

Forward forwarding, such as return "forward:/hello"; The address bar of the browser does not change, but a view is returned to

Redirect redirect, such as return "redirect:/hello"; The browser's address bar will change.

Network explanation:

The spring controller finally returns a Modelandview (urlname), where Urnamel can be a view name that is parsed by the view resolver and writes the response stream back to the client, or through redirect/ Forward: URL mode goes to another controller for processing.

    1. The redirect method is equivalent to "response.sendredirect ()". This way the external feature is that the path to the last display in the browser's address bar is the new path after forwarding. The way it works, The server will first send a response to the browser, then the browser receives this response and then sends a requeset to the server, and then the server sends a new response to the browser. The request received by the page is a new one from the browser. The result of this approach is:

    • A. There are two different request objects before and after forwarding, and the parameters (Request.getparameter ()) and the request attribute (Request.getattribute ()) on the request are not shared between the forward and backward two controllers.

    • B. If the two controllers before and after forwarding are configured within the Spring interceptor, the interceptor intercepts the two request and intercepts two times;

    • C. After the last return to the browser, because the address bar displays the forwarded URL, the Refresh page will only execute the following URL-mapped controller.

    1. forward method is equivalent to Request.getrequestdispatcher (). forward (request,response), the external feature of this approach is that the path that the browser address displays is the path before forwarding. The way it works, forward   occurs inside the server, after the previous controller has finished processing, Go directly to the next controller processing and send the last response to the browser. The result of this approach is:

    • A. Forwarding is the same request before and after a controller can share the parameters and attributes of the previous controller;

    • B. Because it is the same request, the Interceptor will only intercept the previous URL, if the previous URL is not configured at the time of the mapping to interceptor interception, then intercept a URL, that is, intercept only once;

    • C. After the last return to the browser, because the address bar shows the pre-forwarding URL, the page will be refreshed in turn, followed by two controllers.

      This is a note previously recorded in other article systems and is now dumped into CSDN.

      This article is reproduced others articles, but the reprint address has not been recorded before, please forgive me, if there is any infringement of your interests please contact me, I will be the first time to delete.

Spring forwards forward and redirect redirect

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.