springmvc 整合 thymeleaf 模板引擎
第一步:导入 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">
<!-- 设置首页位置-->
<welcome-file-list>
<welcome-file>/login.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
第二步:配置 spring-mvc.xml 文件 ->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--自动扫描包,让指定包下的注解生效,由IOC统一管理-->
<context:component-scan base-package="com.hkl" />
<!--让springmvc不处理静态资源,相当于一个过滤器。 静态资源例如:.css .js .html .mp3 .mp4-->
<mvc:default-servlet-handler/>
<!--之前我们想要完成url的处理以及Controller的适配,则需要配置handlermapping和handelAdepter这两个处理器并且需要将相应的Controller注入进来才可以。但是我们现在只需要开启annotation-driven即可完成,并且会自动注入Controller相应的bean-->
<mvc:annotation-driven/>
<bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" /> <!--这里配置前后缀,按自己的文件位置来配-->
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML" />
<property name="cacheable" value="false" />
<property name="characterEncoding" value="UTF-8"/>
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<!-- 视图解析器-->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8"/>
</bean>
</beans>
第三步:导入 Thymeleaf 相关 pom 依赖 ->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
第四步:先测试一下 controller 是否能接收到前端的请求,写好一个 TestController.java 和一个 index.jsp ->
TestController.java ->
package com.hkl.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping("/test")
public void test() {
System.out.println("接收成功");
}
}
index.jsp ->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="test">test跳转</a>
</body>
</html>
点击超链接 [test跳转] 如果接收不到,就检查一下 lib 目录是否存在 ,如下所示 ->
第五步:测试 thymeleaf 解析器是否成功跳转 ->
TestController.java 文件如下 ->
package com.hkl.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping("/test")
public String test(Model model) {
model.addAttribute("msg","跳转成功");
return "index";
}
}
创建 login.html 和 index.html [ 我的 login.html 直接放在 web 目录下,index.html 放在 web/WEB-INF/templates 目录下 ]
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<a href="test">登录</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<p th:text="${msg}"></p>
</body>
</html>