模板引擎Thymeleaf

类似于jsp的模板引擎

pom.xml中引入依赖:

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

ThymeleafProperties.class:

private static final Charset DEFAULT_ENCODING;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";

将html放在classpath:/templates/

所有的html元素都可以被thymeleaf接管,th:元素名

​ 文档:Tutorial: Using Thymeleaf 第4节:表达式;第10节:转换格式

<!--
model.addAttribute("msg","<h1>hello</h1>");
-->
<!--转译-->
<div th:text="${msg}"></div>
<!--不转译-->
<div th:utext="${msg}"></div>

<!--
model.addAttribute("users", Arrays.asList("xiaoli","xiaowang"));
-->
<!--一下俩种写法效果一致-->
<h3 th:each="user:${users}" th:text="${user}"></h3>
<h3 th:each="user:${users}">[[ ${user} ]]</h3>
posted @ 2022-04-19 20:05  清水煮岁月  阅读(23)  评论(0编辑  收藏  举报