1,建一个springboot项目,目录结如下:
配置pom.xml,添加thymeleaf依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2,在properties文件中配置thymeleaf(主要配置访问路径,编码,和缓存)别忘了这里端口号用的是8082
# thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.cache=false
server.port=8082
3,添加controller类
package design.biye.restfulcurd01.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class TestThymeleafController {
@RequestMapping(value = "/index")
public String index(){
return "index";
}
@RequestMapping("/test")
public String test(ModelMap map){
String resText = "这是测试thymeleaf是否成功的页面,这段文本是在控制器中返回的,看到该行文字代表thymeleaf动态变量成功了";
map.addAttribute("name",resText);
return "test";
}
}
这里index是静态页面,test是动态页面4,添加index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h1> Hello, thymeleaf, index page...</h1>
<p>真好,没有乱码</p>
<p style="color:yellowgreen">我是【俺本熵减】毕业设计</p>
</body>
</html>
添加test.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="${name}"> 这是测试thymeleaf是否成功的页面,本行文字是thymeleaf没有替换掉的文本 </p>
<p style="color:yellowgreen">我是【俺本熵减】毕业设计</p>
</body>
</html>
index页面返回结果
test页面返回结果
需要做毕业设计【赠送源码讲解】,或者做开发入门引导的朋友联系我哟,哈哈~