1在pom.xml中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2在application中添加配置文件
#整合thymeleaf相关配置
#开发时关闭缓存,不然没法看见实时页面
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
#thymeleaf路径
spring.thymeleaf.prefix=classpath:/templates/tl/
#thymeleaf编码格式
spring.thymeleaf.encoding=UTF-8
#thymeleaf类型
spring.thymeleaf.servlet.content-type=text/html; charset=utf-8
#thymeleaf名称后缀
spring.thymeleaf.suffix=.html
注意: thymeleaf的路径,我在templates中新建了一个文件夹 tl
里面随便建了一个index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
模版搜索引擎thymeleaf index.html
</body>
</html>
在tl中新建一个admin的文件夹,里面新建一个info.html的页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
模版搜索引擎thymeleaf index.html
</body>
</html>
3
新建类ServiceSetting
然后在application.properties中加入注解
4在controller中新建一个包thymeleaf,在里面新建一个ThymeleafController
package com.example.demo.controller.thymeleaf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.domain.ServiceSetting;
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {
@Autowired
private ServiceSetting setting;
/**
* 接口访问地址:localhost:8080//thymeleaf/hello
*
*
* @return
*/
@GetMapping("hello")
public String index() {
return "index";//不用加后缀,在配置文件中已经配置了后缀
}
/**
* 接口访问地址:localhost:8080//thymeleaf/info
*
*
* @param modelMap
* @return
*/
@GetMapping("info")
public String admin(ModelMap modelMap) {
modelMap.addAttribute("setting",setting);
return "admin/info";//不用加后缀,在配置文件中已经配置了后缀
}
}
5run as application ,可以查看对应的结果