一、pom.xml配置
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>2.0.1.RELEASE</version> </dependency> |
二、编写Controller
三、建html文件在此文件夹下
使用格式如下
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>小莉莉</h1> <h1 id="" class="" th:text="${admin}">这里是编写XXX的信息</h1> <hr/> <span th:text="${book.name}"></span><br/> <span th:text="${book.author.age}"></span> <hr/> <span th:text="${session.book.name}"></span><br/> <!--默认是request的作用域是不需要添加的--> <span th:text="${admin}"></span> <hr/> <span th:text="${authors[1].name}"></span> <span th:text="${maps.get('kevin').name}"></span> <hr/> <h4>基本选择结构的使用</h4> <span th:if="${book.author.age} >=20">你老了</span> <span th:unless="${book.author.age} >=20">你还是小鲜肉</span> <h4>switch选择结构的使用</h4> <div th:switch="${book.author.age}"> <span th:case="1">超滤机</span> <span th:case="2">拖拉机</span> <span th:case="*">小母鸡</span> </div> <hr/> <h4>循环的使用</h4> <select > <option th:each="author, authorstart :${authors}" th:value="${authorstart.index}" th:text="${author.name}"></option> </select> <hr/> <h4>单选按钮</h4> <input th:checked="${book.author.role==1}" type="radio" name="love">莉莉 <input th:checked="${book.author.role==2}" type="radio" name="love">姐姐 </body> </html> |
四、application中编写关闭缓存和格式要求简单化
spring.thymeleaf.cache=false spring.thymeleaf.mode=LEGACYHTML5 |