您的位置:首页 > 编程语言 > Java开发

SpringBoot整合Thymeleaf

2020-03-28 18:57 155 查看

SpringBoot整合Thymeleaf

  1. 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  1. 基础配置
#Thymeleaf
#是否启用缓存,开发环境不建议启用,生成环境建议启用
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
  1. 创建模板(thymeleaf.html)
<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Thymeleaf 模板</title>
</head>
<body>
<h2>Thymeleaf 模板</h2>
</body>
</html>
  1. 创建Controller
/**
* Author: avaos
*/
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {

@Autowired
private ServerConstant settings;

@GetMapping
public String index(ModelMap modelMap){
modelMap.addAttribute("settings", settings);
return "thymeleaf";
}
  1. 测试
    在浏览器中访问
    http://localhost/thymeleaf
  • 点赞
  • 收藏
  • 分享
  • 文章举报
avaos 发布了43 篇原创文章 · 获赞 1 · 访问量 630 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: