Forward and redirect in Spring MVC

Source: Internet
Author: User

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

  1. <Bean class="Org.springframework.web.servlet.view.InternalResourceViewResolver">
  2. <description>
  3. If the logic tries to name "Hello", Viewresolver will parse into/web-inf/jsp/hello.jsp
  4. </Description>
  5. <property name="order" value="ten" />
  6. <property name= "prefix" value="/web-inf/jsp/" />
  7. <property name="suffix" value= ". jsp" />
  8. </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 contrllor, how to do? We can use the forward prefix to achieve the purpose of forwarding to other resources:

    1. Public String handle () {
    2. //Return "Forward:/hello" and forward to controller capable of matching/hello
    3. //return "Hello" + is actually forwarding, except that the framework will find the view and render
    4. //return "/hello" = with return "Hello"
    5. return "Forward:/hello";
    6. }

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

    1. Public String handle () {
    2. //Redirect to/hello resource
    3. return "Redirect:/hello";
    4. }

Forward and redirect in Spring MVC

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.