1. 程式人生 > 其它 >模板引擎Thymeleaf

模板引擎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>