模板引擎Thymeleaf
阿新 • • 發佈:2022-04-19
類似於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>