Table of contents
1. Conditional judgment and iterative traversal
2. Obtain the data and URL writing in the domain
1. Conditional judgment and iterative traversal
1.1 Conditional judgment
Grammar function
th:if conditional judgment
Prepare data
model.addAttribute("sex","男");
Usage examples
<div>
<span th:if="${sex}=='Female'">This is a girl< ;/span>
<span th:if="${sex}=='Male'">This is a boy</span>< /span>
</div>
operation result:
Of course, th:case is also equivalent to switch in Java.
adding data
model.addAttribute("id",2);
Usage examples
<div th:switch="${id}">
<span th:case="1">id为1</span>
<span th:case="2">id为2</span>
<span th:case="3">id为3</span>
<span th:case="*">id为*</span>
</div>
operation result
2.2 Iterative traversal
Write entity class
package com.example.springbootdemo2.pojo;
public class User {
private int id;
private String name;
private int age;
public User() {
}
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Prepare data
// 添加List列表集合
User user1 = new User(1,"张三",100);
User user2 = new User(2,"李四",90);
User user3 = new User(3,"王五",60);
User user4 = new User(4,"老六",29);
List<User> users = new ArrayList<>();
users.add(user1);
users.add(user2);
users.add(user3);
users.add(user4);
model.addAttribute("users",users);
Display data on the page and cooperate with state variables
thymeleaf encapsulates the traversed state variables into an object, and the state variables can be obtained through the properties of the object:
State variables | meaning |
---|---|
index | The index of the current iterator, starting from 0 |
count | The count of the current iteration object, starting from 1 |
size | The length of the iterated object |
odd/even | Boolean value, whether the current loop is even/odd, starting from 0 |
first | Boolean value, whether the current loop is the first one, if so, return true, otherwise return false |
last | Boolean value, whether the current loop is the last one, if so, return true, otherwise return false |
Usage examples
<table border="1">
<tr>
<th>id</th> 23> ;/table> }"></td> =20> <td th:text="${status.odd}"></td> count}"></td> a i=17> <td th:text="${status.index}"></td> }"></td> ;td th:text="${user.getId()}"></td> <th>Boolean value, whether the current loop is the last one, if so, return true, otherwise return false</th> true, otherwise return false</th> <th>Boolean value, whether the current loop is an odd number, starting from 0</th> <th>Boolean value, whether the current loop is an even number, starting from 0</th> . th>The length of the iterated object</th>
; The index of the current iterator, starting from 0</th>
operation result:
Traverse Map
Prepare data
// 添加map集合数据
Map<String,User> userMap = new HashMap<>();
userMap.put("user1",user1);
userMap.put("user2",user2);
userMap.put("user3",user3);
userMap.put("user4",user4);
model.addAttribute("userMap",userMap);
Use examples
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Key</th>
</tr>
<tr th:each="m : ${userMap}">
<td th:text="${m.value.getId()}"></td>
<td th:text="${m.value.getName()}"></td>
<td th:text="${m.value.getAge()}"></td>
<td th:text="${m.key}"></td>
</tr>
</table>
operation result:
2. Obtain the data and URL writing in the domain
2.1 Get data in the domain
thymeleaf can also obtain data in the request, session, and application fields. The method is as follows:
Prepare data
// Set data in the request field
req.setAttribute("req","request");
// Go to the response field Set data
session.setAttribute("session","session");
// Set data to the application domain
session.getServletContext().setAttribute("app","application");
Usage examples
request domain acquisition method 1: <span th:text="${#request.getAttribute('req')}"></span>
request domain acquisition method 2: <span th:text="${#httpServletRequest.getAttribute('req')}"></span>
<hr>
Session domain acquisition method 1: <span th:text="${#session.getAttribute('session')} "></span>
Session domain acquisition method 2: <span th:text="${#httpSession.getAttribute('session')} "></span>
<hr>
application domain acquisition method 1: <span th:text="${application. app}"></span>
Application domain acquisition method 2: <span th:text="${#servletContext.getAttribute('app' )}"></span>
<hr>
operation result:
2.2 URL writing method
In Thymeleaf, the path is written as @{path}. You can also add parameters to the path and use RestFul style URL.
Prepare data
model.addAttribute("id",100);
model.addAttribute("name","lyl");
Add jump path
@GetMapping("/show2")
@ResponseBody
public String showPage2(String id,String name){
return id+":"+name;
}// @RestFul风格传递参数
@GetMapping("/show3/{id}/{name}")
@ResponseBody
public String showPage3(@PathVariable String id,@PathVariable String name){
return id + ":" + name;
}
Usage examples
<a th:href="@{https://www.baidu.com}">Baidu</a>
<a th: href="@{show2?id=1&name='lyl'}">Static parameter one</a>
<a th: href="@{show2(id=1,name='hqx')}">Static parameter two</a>
<a th :href="@{'show2?id='+${id}+'&name='+${name}}">Dynamic parameters一</a>
<a th:href="@{show2(id=${id},name=${name})}">Dynamic parameters 2</a>
<a th:href="@{show3/{id}/{name}(id=${id},name=${name}) }">RestFul style passing parameters</a><hr>
operation result
3. Related configurations
Thymeleaf related configuration can be performed in the Springboot configuration file
Configuration items | meaning |
---|---|
spring.thymeleaf.prefix | view prefix |
spring.thymeleaf.suffix | view suffix |
spring.thymeleaf.encoding | Encoding format |
spring.thymeleaf.servlet.content-type | response type |
spring.thymeleaf.cache=false | Page caching, if configured to false, page caching will not be enabled, which is convenient for testing. |