[SpringMVC] SpringMVC view, view controller and view parser

1. View of SpringMVC

The view in SpringMVC is the View interface. The role of the view is to render data and display the data in the model to the user. There are many types of SpringMVC views. There are forwarding views and redirecting views by default. When the project introduces jstl dependencies, the forwarding view will be automatically converted
to If the view technology used by JstlView
is Thymeleaf, the Thymeleaf view resolver is configured in the SpringMVC configuration file, and the ThymeleafView is obtained after the view resolver resolves.
1. ThymeleafView view
When the view name set in the controller method does not have any prefix, the view name at this time will be parsed by the view resolver configured in the SpringMVC configuration file, and the view name is spliced ​​with the view prefix and the view suffix to get the final result The path will be redirected by forwarding.

@RequestMapping("/test_view")
public String test_view(){
    
    
    return "test_view";
}
@Controller
public class ViewController {
    
    
    @RequestMapping("/testThymeleafView")
    public String testThymeleafView(){
    
    
        return "success";
    }
}
<a th:href="@{/testThymeleafView}">测试Thymeleaf视图</a>

insert image description here
2. Forward view
in SpringMVCThe default forwarding view is InternalResourceView.
The case of creating a forwarding view in SpringMVC:
When the view name set in the controller method is prefixed with "forward:", the InternalResourceView view is created, and the view name at this time will not be parsed by the view resolver configured in the SpringMVC configuration file , but the prefix "forward:" will be removed, and the remaining part will be used as the final path to realize the jump through forwarding.
Such as: "forward:/", "forward:/employee"

@RequestMapping("/testForward")
public String testForward(){
    
    
    return "forward:/testThymeleafView";
}
<a th:href="@{/testForward}">测试InternalResourceView视图</a><br>

insert image description here
3. Redirect view
in SpringMVCThe default redirect view is RedirectView.
When the view name set in the controller method is prefixed with "redirect:", the RedirectView view is created. The view name at this time will not be parsed by the view resolver configured in the SpringMVC configuration file, but will be prefixed with "redirect :"Removed, the remaining part is used as the final path to realize the jump through redirection.
For example, "redirect:/", "redirect:/employee"
Note:
When parsing the redirection view, the redirect: prefix will be removed first, and then it will judge whether the remaining part starts with /, and if so, the context path will be automatically spliced

@RequestMapping("/testRedirect")
public String testRedirect(){
    
    
    return "redirect:/testThymeleafView";
}
<a th:href="@{/testRedirect}">测试RedirectView视图</a><br>

insert image description here
Click on the hyperlink to jump to the redirected view.
insert image description here

2. The view controller of SpringMVC

1. Function : Realize the mapping relationship between the request address and the view.
2. Conditions of use : When there is no other request processing in the controller method corresponding to the current request mapping, only a view name needs to be set.
3. How to use : Add the SpringMVC view controller in the SpringMVC configuration file, and enable the SpringMVC annotation driver. At this time, the controller method can be annotated.

<!--配置SpringMVC的视图控制器-->
<!--path: 设置处理的请求地址		view-name:设置请求地址所对应的视图名称-->
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
<!--开启SpringMVC的注解驱动(当使用视图控制器后,其他的请求映射将会失效)-->
<mvc:annotation-driven/>
@Controller
public class TestController {
    
    
	//当springmvc的配置文件中使用了视图控制器时,对应的视图控制器的控制方法就可以注释
    /*@RequestMapping("/")
    public String index(){
        return "index";
    }*/
}

3. View parser

Currently, Thymeleaf technology is commonly used for view analysis. InternalResourceView is usually used for jsp view analysis technology. The view resolver is responsible for parsing the view, and the view resolver is configured in the springmvc configuration file.
insert image description here
(1) springMVC.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"
       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">
	<!--开启包扫描-->
    <context:component-scan base-package="com.jd.mvc.controller"/>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <!--视图前缀-->
        <property name="prefix" value="/WEB-INF/templates/"/>
        <!--视图后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

(2)web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--配置编码过滤器-->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--配置SpringMVC的前端控制器-->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

(3)JspController.java

@Controller
public class JspController {
    
    
    @RequestMapping("/success")
    public String success(){
    
    
        return "success";
    }
}

(4)index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>我是首页</h1>
<%--${pageContext.request.contextPath}代表上下文--%>
<a href="${pageContext.request.contextPath}/success">success.jsp</a>
</body>
</html>

(5) pom.xml dependency

<packaging>war</packaging>
<dependencies>
    <!--SpringMVC-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.1</version>
    </dependency>
    <!--日志-->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>
    <!--servletAPI-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <!--Spring与Thymeleaf整合-->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring5</artifactId>
        <version>3.0.12.RELEASE</version>
    </dependency>
</dependencies>

Deploy the project to tomcat, and then run
insert image description here
it Click on the hyperlink to jump to the page
insert image description here

Guess you like

Origin blog.csdn.net/weixin_46081857/article/details/122223258