- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <table>
- <tr>
- <td>idtd>
- <td>姓名td>
- <td>年龄td>
- tr>
- <tr th:each="user:${userList}">
- <td th:text="${user.id}">td>
- <td th:text="${user.name}">td>
- <td th:text="${user.age}">td>
- tr>
- table>
-
- body>
- html>
- package com.xdu.studyspringboot.controller;
-
- import com.xdu.studyspringboot.pojo.User;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import javax.servlet.http.HttpServletRequest;
- import java.util.ArrayList;
- import java.util.List;
-
- @Controller
- public class ThymeleafController {
- @RequestMapping("/eachList")
- public String testEachList(HttpServletRequest request){
- List
users = new ArrayList<>(); - users.add(new User(1, "Tom", 22));
- users.add(new User(2, "Jack", 21));
- users.add(new User(3, "Bob", 23));
-
- request.setAttribute("userList", users);
- return "test";
- }
- }
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <table>
- <tr>
- <td>idtd>
- <td>姓名td>
- <td>年龄td>
- tr>
- <tr th:each="user:${userArray}">
- <td th:text="${user.id}">td>
- <td th:text="${user.name}">td>
- <td th:text="${user.age}">td>
- tr>
- table>
-
- body>
- html>
- package com.xdu.studyspringboot.controller;
-
- import com.xdu.studyspringboot.pojo.User;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import javax.servlet.http.HttpServletRequest;
-
- @Controller
- public class ThymeleafController {
- @RequestMapping("/eachArray")
- public String testEachArray(HttpServletRequest request){
- User[] users = new User[3];
- users[0] = new User(1, "Tom", 22);
- users[1] = new User(2, "Jack", 21);
- users[2] = new User(3, "Bob", 23);
-
- request.setAttribute("userArray", users);
- return "test";
- }
- }
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <table>
- <tr>
- <td>idtd>
- <td>姓名td>
- <td>年龄td>
- tr>
- <tr th:each="user:${userMap}">
- <td th:text="${user.value.id}">td>
- <td th:text="${user.value.name}">td>
- <td th:text="${user.value.age}">td>
- tr>
- table>
-
- body>
- html>
- package com.xdu.studyspringboot.controller;
-
- import com.xdu.studyspringboot.pojo.User;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import javax.servlet.http.HttpServletRequest;
- import java.util.HashMap;
- import java.util.Map;
-
- @Controller
- public class ThymeleafController {
- @RequestMapping("/eachMap")
- public String testEachMap(HttpServletRequest request){
- Map
map = new HashMap<>(); - map.put("user1", new User(1, "Tom", 22));
- map.put("user2", new User(2, "Jack", 21));
- map.put("user3", new User(3, "Bob", 23));
-
- request.setAttribute("userMap", map);
- return "test";
- }
- }
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <table>
- <tr>
- <td>idtd>
- <td>姓名td>
- <td>年龄td>
- tr>
- <div th:each="userMap:${userListMap}">
- <tr th:each="user:${userMap}">
- <td th:text="${user.value.id}">td>
- <td th:text="${user.value.name}">td>
- <td th:text="${user.value.age}">td>
- tr>
- div>
- table>
-
- body>
- html>
- package com.xdu.studyspringboot.controller;
-
- import com.xdu.studyspringboot.pojo.User;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import javax.servlet.http.HttpServletRequest;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- @Controller
- public class ThymeleafController {
- @RequestMapping("/eachListAndMap")
- public String testEachListAndMap(HttpServletRequest request){
- List
-
-
- Map
map1 = new HashMap<>(); - map1.put("user1.1", new User(1, "Tom", 22));
- map1.put("user1.2", new User(2, "Jack", 21));
-
- Map
map2 = new HashMap<>(); - map2.put("user2.1", new User(3, "Bob", 23));
- map2.put("user2.2", new User(3, "Rose", 22));
-
- users.add(map1);
- users.add(map2);
-
- request.setAttribute("userListMap", users);
- return "test";
- }
- }
如果为真,则显示该语句
注:没有else语句
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <p th:if="${age}>18">
- 已成年
- p>
-
- <p th:if="${isLogin}">
- 已登陆
- p>
- body>
- html>
- package com.xdu.studyspringboot.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import javax.servlet.http.HttpServletRequest;
-
- @Controller
- public class ThymeleafController {
- @RequestMapping("/if")
- public String testIf(HttpServletRequest request){
- request.setAttribute("age", 20);
- request.setAttribute("isLogin", true);
- return "test";
- }
- }
如果为假,则显示该语句
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <p th:unless="${age}<18">
- 未成年
- p>
-
- <p th:unless="${isLogin}">
- 未登陆
- p>
- body>
- html>
- package com.xdu.studyspringboot.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import javax.servlet.http.HttpServletRequest;
-
- @Controller
- public class ThymeleafController {
- @RequestMapping("/unless")
- public String testUnless(HttpServletRequest request){
- request.setAttribute("age", 20);
- request.setAttribute("isLogin", false);
- return "test";
- }
- }
结果1
结果2
默认结果
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <div th:switch="${sex}">
- <p th:case="man">男p>
- <p th:case="woman">女p>
- <p th:case="*">未知p>
- div>
- body>
- html>
- package com.xdu.studyspringboot.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import javax.servlet.http.HttpServletRequest;
-
- @Controller
- public class ThymeleafController {
- @RequestMapping("/switch")
- public String testSwitch(HttpServletRequest request){
- request.setAttribute("sex", "man");
-
- return "test";
- }
- }
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
-
- <p th:text="${12 + 13}">结果p>
-
-
- <p th:if="${age > 18}">已成年p>
-
-
- <p th:if="${name == null}">用户名为空p>
- <p th:if="${name != null}">用户名不为空p>
- <p th:if="${name eq null}">用户名为空p>
- <p th:if="${name ne null}">用户名不为空p>
-
-
- <p th:text="${isLogin == true ? '用户已登录' : '用户未登录'}">p>
-
- <p th:text="${isLogin == true ? (age > 18 ? '用户已成年' : '用户未成年') : '用户未登录'}">p>
- body>
- html>
- package com.xdu.studyspringboot.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import javax.servlet.http.HttpServletRequest;
-
- @Controller
- public class ThymeleafController {
- @RequestMapping("/math")
- public String testMath(HttpServletRequest request){
- request.setAttribute("name","Tom");
- request.setAttribute("age",22);
- request.setAttribute("isLogin", true);
- return "test";
- }
- }