一、IDEA中SpringBoot+Thymeleaf配置
1、pom添加以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、html文件配置
开始idea中html直接用thymeleaf表达式,会出现红线,这时你可以用Alt+Enter,html会引入<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" >
3、使用注意
需要将文件放入目录:“classpath:/templates/”;
文件的后缀:".html";
二、Thymeleaf语法
1、注释
单行注释:!-- 你看不到我~~~~~ -->
多行注释:
<!--/*
你看不到我~~~
*/-->
2.1、使用前缀th
Thymeleaf使用属性都需加前缀th,如<span th:text="wswdwf"></span>
2.2、th作用
th就是替换原有html的值:th:html属性名=值;
Controller代码
home.html文件中的代码
网页中输出的结果
3、读取变量
${变量名},
如
<div th:text="${color}"></div>
4、迭代循环
th:each
<div th:each="color:${colors}">
<div th:text="${color}"></div>
</div>
5、text和utext的区别
Controller代码
th:text :
语法:
<p th:text="${welcome}"></p>
若传进的变量带有html标签,不会被解析
结果为
th:utext :
语法:
<p th:utext="${welcome}"></p>
用utext,会解析html,显示相应的效果
结果为
小结
用th:text不会解析html,用th:utext会解析html,在页面中显示相应的样式