SpringBoot学习笔记(四):Thymeleaf基本使用
Thymeleaf官网
Thymeleaf参考指南
一、Thymeleaf的简单介绍
- Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎,能够处理HTML,XML,JavaScript,CSS甚至纯文本。
二、Thymeleaf标准表达式语法
- 1.变量表达式:${}
- 2.选择或星号表达式:*{}
- 3.文字国际化表达式:#{}
- 4.URL 表达式:@{}
三、pom.xml引入Maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
四、application.properties配置thymeleaf
#关闭thymeleaf缓存
spring.thymeleaf.cache=false
<html lang="en" xmlns:th="http://www.thymeleaf.org">
五、控制类
@Controller
public class HelloController {
@RequestMapping("/")
public String index(ModelMap map) {
map.addAttribute("message", "Hello World!");
return "hello";
}
}
@Controller
public class ExampleController {
@RequestMapping("/string")
public String string(ModelMap map) {
map.addAttribute("userName", "Jax");
return "string";
}
@RequestMapping("/if")
public String ifunless(ModelMap map) {
map.addAttribute("flag", "yes");
return "if";
}
@RequestMapping("/list")
public String list(ModelMap map) {
map.addAttribute("users", getUserList());
return "list";
}
@RequestMapping("/url")
public String url(ModelMap map) {
map.addAttribute("type", "more");
map.addAttribute("img", "https://www.baidu.com/img/flexible/logo/pc/privacy.gif");
return "url";
}
@RequestMapping("/eq")
public String eq(ModelMap map) {
map.addAttribute("name", "example");
map.addAttribute("age", 30);
map.addAttribute("flag", "yes");
return "eq";
}
@RequestMapping("/switch")
public String switchcase(ModelMap map) {
map.addAttribute("sex", "woman");
return "switch";
}
private List<User> getUserList(){
List<User> list=new ArrayList<User>();
User user1=new User("Tom",21,"123");
User user2=new User("Jenny",22,"124");
User user3=new User("Alice",20,"125");
list.add(user1);
list.add(user2);
list.add(user3);
return list;
}
}
六、template包下的html文件
<h1 th:text="${message}">Hello World</h1>
<h1>name</h1>
<p th:text="${userName}">example</p>
<span th:text="'Welcome ' + ${userName} + '!'"></span>
<br/>
<span th:text="|Welcome ${userName}!|"></span>
<h1>If/Unless</h1>
<a th:if="${flag == 'yes'}" th:href="@{http://www.baidu.com/}"> 百度 </a>
<br/>
<a th:unless="${flag != 'no'}" th:href="@{http://www.runoob.com/}" >菜鸟教程</a>
<tr th:each="user,iterStat : ${users}">
<td th:text="${user.name}">example</td>
<td th:text="${user.age}">6</td>
<td th:text="${user.pass}">213</td>
<td th:text="${iterStat.index}">index</td>
</tr>
<h1>URL</h1>
<a th:href="@{http://www.baidu.com/{type}(type=${type})}">link1</a>
<br/>
<div th:style="'background:url(' + @{${img}} + ');'">
<br/><br/><br/>
</div>
<h1>EQ</h1>
<input th:value="${name}"/>
<br/>
<input th:value="${age gt 30 ? '中年':'年轻'}"/>
<br/>
<a th:if="${flag eq 'yes'}" th:href="@{http://www.runoob.com/}"> 菜鸟 </a>
<div th:switch="${sex}">
<p th:case="'woman'">她是女的!</p>
<p th:case="'man'">他是男的!</p>
<p th:case="*">不男不女!</p>
</div>