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:
- <Bean class="Org.springframework.web.servlet.view.InternalResourceViewResolver">
- <description>
- If the logic tries to name "Hello", Viewresolver will parse into/web-inf/jsp/hello.jsp
- </Description>
- <property name="order" value="ten" />
- <property name= "prefix" value="/web-inf/jsp/" />
- <property name="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 contrllor, how to do? We can use the forward prefix to achieve the purpose of forwarding to other resources:
- Public String handle () {
- //Return "Forward:/hello" and forward to controller capable of matching/hello
- //return "Hello" + is actually forwarding, except that the framework will find the view and render
- //return "/hello" = with 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";
- }
Forward and redirect in Spring MVC