模版引擎就是将模型数据和动态模版页面转成html静态页面的一种技术。
springboot对Freemarker和Thymeleaf支持最友好;
第一步:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>第二步:
thymeleaf对标签语法要求比较严格,如果有标签忘记闭合,那么会在访问该页面的时候报错,解决办法如下:
1:pom依赖
<dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.22</version> </dependency>2:在application.properties中加如下配置
spring.thymeleaf.mode = LEGACYHTML5改为LEGACYHTML5可以得到一个可能更友好亲切的格式要求。
第三步:Thymeleaf视图解析:
HTML文件是在templates目录下的,可以修改默认视图解析:
spring.thymeleaf.prefix=classpath:/templates/html spring.thymeleaf.suffix=.html #默认情况下,页面会被浏览器缓存,开发阶段设置为false spring.thymeleaf.cache=false第四: 在页面中访问静态资源:
<img src=”/images/….”> <script src=”/js/….”> <link href=”/css/….”>
注意:无需带上static目录;
thymeleaf模版语法:循环、判断、表达式、日期处理等等
一:数据填充到模板中
@RequestMapping("/index") public String hello(Model map){ //map Map<String, Object> student= new HashMap<>(); student.put("name", "张三丰"); map.addAttribute("student",student); return "index"; } <span th:text="${student.name}"></span> 字符串拼接:<h2 th:text="'姓名:'+${student.name}"></h2> 三元表达式:<input th:value="${age gt 30 ? '中年':'年轻'}"/> gt:great than(大于) ge:great equal(大于等于) eq:equal(等于) lt:less than(小于) le:less equal(小于等于) ne:not equal(不等于)记住:th:text就是把数据渲染到两个标签中间
<img th:src="${book.bookUrl}"/> <span th:text="${book.bookName}"/>
记住:如果你想让标签属性值动态化,请在标签属性名前面加上th: 如th:src
二:循环遍历list集合
<table border="1px" cellspacing="0px" cellspadding="0px" width="100%"> <tr> <td>编号</td><td>书名</td><td>书价格</td><td>图片地址</td> </tr> <tr th:each="book:${books}"> <td>编号</td> <td th:text="${book.bookName}">书名</td> <td th:text="${book.bookPrice}">书价格</td> <td th:text="${book.bookUrl}">图片地址</td> </tr> </table>取循环下标:
<tr th:each="user,userStat : ${list}"> <th th:text="${userStat.index}">状态变量:index</th> <th th:text="${userStat.count}">状态变量:count</th> <th th:text="${userStat.size}">状态变量:size</th> <th th:text="${userStat.current.userName}">状态变量:current</th> <th th:text="${userStat.even}">状态变量:even****</th> <th th:text="${userStat.odd}">状态变量:odd</th> <th th:text="${userStat.first}">状态变量:first</th> <th th:text="${userStat.last}">状态变量:last</th> </tr>说明:
index:列表状态的序号,从0开始;
count:列表状态的序号,从1开始;
size:列表状态,列表数据条数;
current:列表状态,当前数据对象
even:列表状态,是否为奇数,boolean类型
odd:列表状态,是否为偶数,boolean类型
first:列表状态,是否为第一条,boolean类型
last:列表状态,是否为最后一条,boolean类型
三:if逻辑判断
<h1> <b th:text="${name}"></b>: <span th:if="${age gt 30}">中年</span> <span th:unless="${age gt 30}">年轻</span> </h1>记住:th:if表示满足条件显示标签,th:unless表示不满足条件显示标签
四:日期处理
<span th:text="${#dates.format(post.postCreate,'yyyy-MM-dd')}">2017年11月8日</span>将Date类型的日期对象转换成指定格式的时间字符串
五:定义片段和引用片段
定义片段:新建footer.html,并在footer.html中声明片段
<footer th:fragment="copy"> <script type="text/javascript" th:src="@{/plugins/jquery/jquery-3.0.2.js}"></script> </footer>引用片段:在index.html中引用footer片段:
<!--下面的footer是页面的名字,不是标签的名字--> <div th:insert="footer::copy"></div> <div th:replace="footer::copy"></div> <div th:include="footer::copy"></div> 结果为: <div> <footer> <script type="text/javascript" th:src="@{/plugins/jquery/jquery-3.0.2.js}"></script> </footer> </div> <footer> <script type="text/javascript" th:src="@{/plugins/jquery/jquery-3.0.2.js}"></script> </footer> <div> <script type="text/javascript" th:src="@{/plugins/jquery/jquery-3.0.2.js}"></script> </div>说明:
th:insert :保留自己的主标签,保留th:fragment的主标签。
th:replace :不要自己的主标签,保留th:fragment的主标签。
th:include :保留自己的主标签,不要th:fragment的主标签。(官方3.0后不推荐)
https://blog.csdn.net/believe__sss/article/details/79992408
六:内联方式
如果不内联我们这么拼接字符串
<span th:text="'名字'+${stu.name}+'性别'+${stu.sex}+'年龄'+${stu.age}"></span>
或
1
<span th:inline="text">名字[[${stu.name}]]性别[[${stu.sex}]]年龄[[${stu.age}]]</span>
注意:一定要加th:inline="text"
<script></script>标签里面不能直接写el表达式,但是很多场景我们必须要写,这个时候就必须使用内联方式
比如:
<script th:inline="javascript"> alert([[${stu.name}]]) </script>七:内置对象
<span th:if="${session.name!=null}" th:text="'欢迎:'+${session.name}"></span> <span th:id="${session.name==null}" th:>请登录</span>session就是内置对象,当然还有其他内置对象,其他内置对象请参考:http://blog.csdn.net/zsl129/article/details/53007192
八:自定义标签属性
这样写标签属性是取不到值的,你必须这么写
1
<main th:attr="sex=${stu.sex},age=${stu.age}"></span>
th:text : 用于文本的显示,并且可以进行简单的计算。
th:utext : 用于html的文本替换。
th:if : 用于判断条件,还可以多条件 and,or(二元操作符),!, not非(一元操作符)。
<div th:if="${user} != null and ${otherUser} != null">show</div>
th:unless : 用于判断条件,与th:if作用相反。
th:switch th:case 用于多个同等级相同目的判断,多选一时使用。
<div th:switch="${user.name}">
<p th:case="maliming">first</p>若${user.name}中的值为maliming则显示,否则不显示
<p th:case="${otherUser.name}">second</p>
</div>
th:action: 用于定义后台控制器的路径,表单的提交地址,相当于<form>标签的action属性。
<form th:action="@{user/login}" method="post"></form>
th:each : List集合循环遍历
<tr th:each="user,userStat:${messages.list}">
<td th:text="${user.name}"></td>
<td th:text="userStat.index"></td>
</tr>
user是临时变量,相当于for(User user : users)中的user,
userStat称为状态变量:
属性有index:当前迭代对象的index(从0开始计算),
count: 当前迭代对象的index(从1开始计算),
size:被迭代对象的大小,
current:当前迭代变量,
even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算),
first:布尔值,当前循环是否是第一个,last:布尔值,当前循环是否是最后一个。
Map集合循环遍历
<div th:each="map:${maps}" th:text="${map}"></div>
数组循环遍历
<tr>
<td th:each="array:${arrays}" th:text="${array}"></td>
</tr>
th:value : 用于属性赋值。input 等表单元素th:src : 用于外部资源的引入,例如图片,js文件。th:href; 用于定义超链接,相当于<a></a>标签的href属性。 <a th:href="@{/user/selectUser?(currentPage=1,reTypeid=${reTypeid},inquiry=${inquiry})}"></a> 传参。
th:remove : 用于删除。可以表达式传参。
<table border="1"> <thead> <tr> <th>编号</th> <th>用户名</th> <th>姓名</th> <th>性别</th> </tr> </thead> <tbody th:remove="all-but-first" 或者表达式th:remove="${choice}? all : all-but-first"> <tr> <td>1</td> <td>xxxxxxxx@qq.com</td> <td>Tom</td> <td>男</td> </tr> <tr> <td>3</td> <td>xxxxxxxx@qq.com</td> <td>Lucy13</td> <td>女</td> </tr>
</tbody>
</table>
这里的<tbody></tbody>标签中有th:remove="all-but-first",意思是只保留<tbody></tbody>标签中的第一个字标签,也就是name为Tom的所在<tr></tr>,其他的子标签则会删除,th:remove有五个属性值。
all:删除所在标签和内容及其所属的所有子标签。body:删除所在标签的内容及其所属的所有子标签。tag:删除所在标签,不删除任何所属的子标签。all-but-first:删除除第一个子标签外的其他子标签。none:不起作用,什么也不做。
th:selected : 用于选择框设置选中值。通常和th:each一起使用。
<select>
<option th:selected="${user.name} == ${otherUser.name}"></option> 若相等就默认选中此<option></option>
</select>
th:object : 用于表单数据对象绑定,后台controller中参数保持一致,和选择(星号)表达式。 <form th:object="${user}"> <input th:value="*{name}"/> *号代替了${user} </form> public ModelAndView addUser(@RequestParam(value = "user") User user,ModelMap model){}
th:attr : 用于设置任意属性 <input th:attr="value=${user.name}"/> 设置单个属性。
<input th:attr="value=${user.username},name=username"/> 设置多个属性之间用逗号隔开。
#dates:日期格式化内置对象,具体方法可以参照java.util.Date; #calendars:类似于#dates,但是是java.util.Calendar类的方法; #numbers: 数字格式化; #strings:字符串格式化,具体方法可以参照java.lang.String,如startsWith、contains等; #objects:参照java.lang.Object; #bools:判断boolean类型的工具; #arrays:数组操作的工具; #lists:列表操作的工具,参照java.util.List; #sets:Set操作工具,参照java.util.Set; #maps:Map操作工具,参照java.util.Map; #aggregates:操作数组或集合的工具; #messages:操作消息的工具。