Spring boot使用thymeleaf

xiaoxiao2021-02-28  36

参考: http://blog.csdn.net/u012706811/article/details/52185345

引入thymeleaf依赖

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

关闭thymeleaf缓存

######################################################## ###THYMELEAF (ThymeleafAutoConfiguration) ######################################################## #spring.thymeleaf.prefix=classpath:/templates/ #spring.thymeleaf.suffix=.html #spring.thymeleaf.mode=HTML5 #spring.thymeleaf.encoding=UTF-8 # ;charset=<encoding> is added #spring.thymeleaf.content-type=text/html # set to false for hot refresh spring.thymeleaf.cache=false

默认视图解析

spring-boot很多配置都有默认配置,比如默认页面映射路径为 classpath:/templates/*.html 同样静态文件路径为 classpath:/static/

编写模板文件

编写模板文件src/main/resouces/templates/hello.html: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <meta charset="UTF-8"></meta> <title>Hello World</title> </head> <body> <h1>thymeleaf demo</h1> <p th:text="${name}"></p> </body> </html>

html模板标签需要闭合

编写模板文件controller

@Controller @RequestMapping("templates") public class TemplatesController { @RequestMapping("thymeleaf") public String thymeleafTemplates(Map<String,Object> map){ map.put("name","tom"); return "Hello"; } }

转载请注明原文地址: https://www.6miu.com/read-2612389.html

最新回复(0)