thymeleaf常见使用

引入thymeleaf  

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


在html中添加

xmlns:th="http://www.thymeleaf.org"

thymeleaf的一些常见操作:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
</head>
<body>
<h1>thymeleaf使用</h1>
<h1 th:text="${admin}">这里是编写XXX的信息</h1>
<hr/>

<span th:text="${book.getName()}">book.name</span><br/>
<span th:text="${book.author.getAge()}">author.name</span>
<hr/>
<span th:text="${session.book.getName()}">session作用域的book</span><br/>

<hr/>
<!--list集合-->
<span th:text="${books.get(1).getName()}">list集合</span><br/>
<!--map集合-->
<span th:text="${map.get('zhangsan').getAge()}">map集合</span>

<h4>基本选择结构的使用</h4>
<!--if-->
<span th:if="${book.author.getAge()}>=20">你老了</span>
<!--取反-->
<span  th:unless="${book.author.age}>=20">你还是小鲜肉</span>
<hr/>
<!--switch case="*"类似于default -->
<div th:switch="${book.author.getAge()}">
    <span th:case="50">超滤机</span>
    <span th:case="30">拖拉机</span>
    <span th:case="*">小母鸡</span>
</div>
<hr/>
<h4>循环的使用</h4>
<!--each有三个参数 book临时变量,状态变量,要循环的数组-->
<!--bookstart称作状态变量,属性有:-->
<!--index:当前迭代对象的index(从0开始计算)-->
<!--count: 当前迭代对象的index(从1开始计算)-->
<!--size:被迭代对象的大小-->
<!--current:当前迭代变量-->
<!--even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算)-->
<!--first:布尔值,当前循环是否是第一个-->
<!--last:布尔值,当前循环是否是最后一个-->
<select>
    <option th:each="book,bookstart :${books}" th:value="${bookstart.index}" th:text="${book.getName()}"></option>
</select>
<br/>
<h3>单选按钮</h3>
<input type="radio" name="bookname" th:checked="${book.getAuthor().getAge()}==20">西游记
<input type="radio" name="bookname" th:checked="${book.getAuthor().getAge()}==50">三国演义



</body>
</html>

这里有一篇比较详细的介绍:http://www.ityouknow.com/ 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值