1、添加jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、配置文件设置
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=LEGACYHTML5
3、java后台代码
@Controller
public class TestController {
@Autowired
TestService testService;
@RequestMapping("/")
public String testThymeleaf(ModelMap modelMap){
modelMap.addAttribute("msg", "helloworld");
return "index";
}
}
4、页面设计
必须要放指定的文件夹下,此处的路径是resource/templates,内容如下
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h1 th:text="${msg}"></h1>
</body>
</html>
网友评论