一.前言
二.整合
1.poml.xml添加包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
2.application.xml配置
spring:
thymeleaf:
mode: LEGACYHTML5
cache: false
suffix: .html
encoding: UTF-8
servlet:
content-type: text/html
prefix: classpath:/templates/
3.测试
在resources文件夹下面新建文件夹templates.新建regist.html文件 regist.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
</head>
<body>
<div>
<div> name: <span th:text="${name}"/></div>
<div> username: <span th:text="${user.username}"/></div>
<div> password: <span th:text="${user.password}"/></div>
</div>
</body>
</html>
2.controller层
@Controller
public class ViewsController {
@GetMapping("/regist")
public ModelAndView regist(){
ModelAndView view = new ModelAndView("regist");
User user = new User("lss0555","123456");
view.addObject("name","lss");
view.addObject("user",user);
return view;
}
}
3.访问
访问 http://localhost:8085/regist 路径 ,结果:
name: lss
username: lss0555
password: 123456