Request forwarding and redirection for spring MVC
1. Request forwarding of spring MVC -- thymeleafView
thymeleafVIew view
If the return value [view name] of the method processing the request is not prefixed, this view will be parsed by the view parser configured by spring MVC. This view is a thymeleaf view. As for why there is such a distinction, it is because the view parser of thymeleaf and the html page display effect are used. If jsp [not used by many people now] is used, the configured view parser is InternalResourceView. Learn about this view below
If you want to realize page Jump, you have to go through many pages. For convenience, you need to use request forwarding. Below, first jump from the home page to the show page, and then jump to the thyme AF view page. [in fact, you can jump directly to the past, just to explain the jump]
In Java
@RequestMapping("/testThymeleafView") public String testThymeleafView(){ return "thymeleafView"; } @RequestMapping("/goToThymeleafView") public String goToThymeleafView(){ return "show"; }
2. Request forwarding of SpringMvc -- InternalResourceView
The default forwarding view of spring MVC is internal resource view
When the prefix [forward:] is added to the return value of the method processing the request, the view name will not be parsed by the view parser configured in the spring MVC configuration file, but the prefix "forward:" will be removed, and the rest will be used as the final path to jump through forwarding. The function it is actually responsible for is also request forwarding. The prefix is not followed by the view name but the request path. However, the return value will be parsed twice during the underlying processing. The return value will be encapsulated as ModelAndView twice, that is, the returned ModelAndView will be parsed as InternalResourceView for the first time and thymeleafView for the second time.
In java
@RequestMapping("/testInternalResourceView") public String testInternalResourceView(){ return "forward:/testThymeleafView"; }
Interpretation:
In the source code
First, use the debug mode to find the line that executes the disPatchservlet method and power off. After entering, you can first see that the received return value [ModelAndView object] is forward:/testThymeleafView
When you execute applyDefaultViewName, you can find that when the VIEWNAME is not empty, the current value will be assigned to VIEWNAME. It is the value just passed in. It can be well understood by associating with the setViewName when using ModelAnderAndView
After reaching the render method, the view name will be resolved
When the resolveViewNames method is executed, the value of the ModelAndView object just returned, that is, the view name, will be parsed. Finally, it will be resolved to InternalResourceView on the line of the tag. In the subsequent processing, obtain the path to be requested [target resource, the path after the suffix]
This is the second time that the returned result is encapsulated into a ModelAndView object. When the requested target resource is obtained, the controller method responsible for processing the requested path in the controller will be called again. Later, the parsed return value of the target path will be encapsulated as thymeleafView again, which will be rendered and displayed on the browser page.
Not much
3. Request redirection of SpringMvc -- RedirectView
RedirectVIew is used to implement the request for redirection. When using it, you need to add [redirect:] before the return value of the controller method. After adding, this return value will not be parsed by the view parser in the SpringMvc configuration file. The prefix will be removed during processing, and the rest will be requested in the form of request redirection.
difference:
When using request forwarding, the address that initiates the request is the same as the address that last appears in the browser address bar. The returned view request path is [/ thymeleafView], which is one request
When request redirection is used, the address of the request is the address resolved by the controller method. After the final forwarding is completed, the request path of the returned view appears on the browser address bar, and the returned view request path [/ thymeleafView] appears on the address bar. Two requests are made.
In java
@RequestMapping("/testRedirect") public String testRedirect(){ // This html page cannot jump directly to the page under the WEB-INF directory, but to a request, which is parsed by the dispatcher servlet and comes to the Controller // After the request is processed by spring MVC, the view name is obtained. Finally, the view parser parses the view and renders it to the browser return "redirect:/testThymeleafView"; }
4. View controller for spring MVC
If the created controller method is only to realize request forwarding and page forwarding, and there is no code with other functions in the method, you can use the view controller in the configuration file of spring MVC. Configuring view controllers with labels
Use: first add the namespace mvc [just copy the first xmlns and add: namespace name, then change the name after the schema to the name of the namespace, then copy the string after the http at the bottom and add it to the label, and change the beans in the string you copied to the namespace name to be added. If an error is reported, see if there are more double quotes and > or [missing]
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
When you add a view controller to the spring mvc configuration file, all controller methods will fail. You need to add a label and turn on the mvc annotation driver to restore normal access
In the configuration file
It is possible that the view name will be red here, because the view is in the WEB-INF, which will not affect the normal access. However, if you want to access other pages through the home page, you need to add a label to open the mvc annotation driver, which is the mvc namespace
<!--Configure view controller--> <mvc:view-controller path="/" view-name="index"></mvc:view-controller> <!--open mvc Annotation driven--> <mvc:annotation-driven/>