1.模板引擎:为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的文档
就是将模板文件和数据通过模板引擎生成一个HTML代码
2.springboot开发中,使用thymeleaf模板引擎,首先在pom文件中的导入依赖
<!-- 使用thymeleaf模板引擎基于3.x开发,需要导入的依赖-->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
3. 使用thymeleaf模板引擎,静态资源必须放在resources/templates
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
// templates目录下的所有页面必须使用@Controller来实现页面跳转
// 需要使用thymeleaf模板引擎支持
@Controller
public class IndexController {
@RequestMapping("/test")
public String test(Model model){
model.addAttribute("msg","hello,springboot");
return "test";
}
}
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--所有的HTMLy元素都可以被thymeleaf替换接管: th: 元素名 -->
<div th:text="${msg}"></div>
</body>
</html>
注意:此项目建议springboot的版本建议不要使用最新版本,否则会出现各种bug
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>