• Thymeleaf学习(2)—— 流程控制


    一. each(循环)

            

    1. 循环List

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <table>
    9. <tr>
    10. <td>idtd>
    11. <td>姓名td>
    12. <td>年龄td>
    13. tr>
    14. <tr th:each="user:${userList}">
    15. <td th:text="${user.id}">td>
    16. <td th:text="${user.name}">td>
    17. <td th:text="${user.age}">td>
    18. tr>
    19. table>
    20. body>
    21. html>
    1. package com.xdu.studyspringboot.controller;
    2. import com.xdu.studyspringboot.pojo.User;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import javax.servlet.http.HttpServletRequest;
    6. import java.util.ArrayList;
    7. import java.util.List;
    8. @Controller
    9. public class ThymeleafController {
    10. @RequestMapping("/eachList")
    11. public String testEachList(HttpServletRequest request){
    12. List users = new ArrayList<>();
    13. users.add(new User(1, "Tom", 22));
    14. users.add(new User(2, "Jack", 21));
    15. users.add(new User(3, "Bob", 23));
    16. request.setAttribute("userList", users);
    17. return "test";
    18. }
    19. }

    2. 循环Array

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <table>
    9. <tr>
    10. <td>idtd>
    11. <td>姓名td>
    12. <td>年龄td>
    13. tr>
    14. <tr th:each="user:${userArray}">
    15. <td th:text="${user.id}">td>
    16. <td th:text="${user.name}">td>
    17. <td th:text="${user.age}">td>
    18. tr>
    19. table>
    20. body>
    21. html>
    1. package com.xdu.studyspringboot.controller;
    2. import com.xdu.studyspringboot.pojo.User;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import javax.servlet.http.HttpServletRequest;
    6. @Controller
    7. public class ThymeleafController {
    8. @RequestMapping("/eachArray")
    9. public String testEachArray(HttpServletRequest request){
    10. User[] users = new User[3];
    11. users[0] = new User(1, "Tom", 22);
    12. users[1] = new User(2, "Jack", 21);
    13. users[2] = new User(3, "Bob", 23);
    14. request.setAttribute("userArray", users);
    15. return "test";
    16. }
    17. }

    3. 循环Map

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <table>
    9. <tr>
    10. <td>idtd>
    11. <td>姓名td>
    12. <td>年龄td>
    13. tr>
    14. <tr th:each="user:${userMap}">
    15. <td th:text="${user.value.id}">td>
    16. <td th:text="${user.value.name}">td>
    17. <td th:text="${user.value.age}">td>
    18. tr>
    19. table>
    20. body>
    21. html>
    1. package com.xdu.studyspringboot.controller;
    2. import com.xdu.studyspringboot.pojo.User;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import javax.servlet.http.HttpServletRequest;
    6. import java.util.HashMap;
    7. import java.util.Map;
    8. @Controller
    9. public class ThymeleafController {
    10. @RequestMapping("/eachMap")
    11. public String testEachMap(HttpServletRequest request){
    12. Map map = new HashMap<>();
    13. map.put("user1", new User(1, "Tom", 22));
    14. map.put("user2", new User(2, "Jack", 21));
    15. map.put("user3", new User(3, "Bob", 23));
    16. request.setAttribute("userMap", map);
    17. return "test";
    18. }
    19. }

    4. 循环List-Map

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <table>
    9. <tr>
    10. <td>idtd>
    11. <td>姓名td>
    12. <td>年龄td>
    13. tr>
    14. <div th:each="userMap:${userListMap}">
    15. <tr th:each="user:${userMap}">
    16. <td th:text="${user.value.id}">td>
    17. <td th:text="${user.value.name}">td>
    18. <td th:text="${user.value.age}">td>
    19. tr>
    20. div>
    21. table>
    22. body>
    23. html>
    1. package com.xdu.studyspringboot.controller;
    2. import com.xdu.studyspringboot.pojo.User;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import javax.servlet.http.HttpServletRequest;
    6. import java.util.ArrayList;
    7. import java.util.HashMap;
    8. import java.util.List;
    9. import java.util.Map;
    10. @Controller
    11. public class ThymeleafController {
    12. @RequestMapping("/eachListAndMap")
    13. public String testEachListAndMap(HttpServletRequest request){
    14. List> users = new ArrayList<>();
    15. Map map1 = new HashMap<>();
    16. map1.put("user1.1", new User(1, "Tom", 22));
    17. map1.put("user1.2", new User(2, "Jack", 21));
    18. Map map2 = new HashMap<>();
    19. map2.put("user2.1", new User(3, "Bob", 23));
    20. map2.put("user2.2", new User(3, "Rose", 22));
    21. users.add(map1);
    22. users.add(map2);
    23. request.setAttribute("userListMap", users);
    24. return "test";
    25. }
    26. }

    二. if

            如果为,则显示该语句

    注:没有else语句

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <p th:if="${age}>18">
    9. 已成年
    10. p>
    11. <p th:if="${isLogin}">
    12. 已登陆
    13. p>
    14. body>
    15. html>
    1. package com.xdu.studyspringboot.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import javax.servlet.http.HttpServletRequest;
    5. @Controller
    6. public class ThymeleafController {
    7. @RequestMapping("/if")
    8. public String testIf(HttpServletRequest request){
    9. request.setAttribute("age", 20);
    10. request.setAttribute("isLogin", true);
    11. return "test";
    12. }
    13. }

    三. unless

            如果为,则显示该语句

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <p th:unless="${age}<18">
    9. 未成年
    10. p>
    11. <p th:unless="${isLogin}">
    12. 未登陆
    13. p>
    14. body>
    15. html>
    1. package com.xdu.studyspringboot.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import javax.servlet.http.HttpServletRequest;
    5. @Controller
    6. public class ThymeleafController {
    7. @RequestMapping("/unless")
    8. public String testUnless(HttpServletRequest request){
    9. request.setAttribute("age", 20);
    10. request.setAttribute("isLogin", false);
    11. return "test";
    12. }
    13. }

    四. switch-case

    结果1

    结果2

    默认结果

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <div th:switch="${sex}">
    9. <p th:case="man">p>
    10. <p th:case="woman">p>
    11. <p th:case="*">未知p>
    12. div>
    13. body>
    14. html>
    1. package com.xdu.studyspringboot.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import javax.servlet.http.HttpServletRequest;
    5. @Controller
    6. public class ThymeleafController {
    7. @RequestMapping("/switch")
    8. public String testSwitch(HttpServletRequest request){
    9. request.setAttribute("sex", "man");
    10. return "test";
    11. }
    12. }

    五. 运算符

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <p th:text="${12 + 13}">结果p>
    9. <p th:if="${age > 18}">已成年p>
    10. <p th:if="${name == null}">用户名为空p>
    11. <p th:if="${name != null}">用户名不为空p>
    12. <p th:if="${name eq null}">用户名为空p>
    13. <p th:if="${name ne null}">用户名不为空p>
    14. <p th:text="${isLogin == true ? '用户已登录' : '用户未登录'}">p>
    15. <p th:text="${isLogin == true ? (age > 18 ? '用户已成年' : '用户未成年') : '用户未登录'}">p>
    16. body>
    17. html>
    1. package com.xdu.studyspringboot.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import javax.servlet.http.HttpServletRequest;
    5. @Controller
    6. public class ThymeleafController {
    7. @RequestMapping("/math")
    8. public String testMath(HttpServletRequest request){
    9. request.setAttribute("name","Tom");
    10. request.setAttribute("age",22);
    11. request.setAttribute("isLogin", true);
    12. return "test";
    13. }
    14. }

  • 相关阅读:
    【CSS】问题:为什么我的z-index不起作用
    MySQL的主从复制、读写分离
    CC++中深浅拷贝(map、vector)与内存释放
    Contact us
    IronPDF for .NET 2022.11.10347
    类和对象:运算符重载
    C#使用ICSharpCode.TextEditor制作代码编辑器
    MySQL特殊字符处理方式
    GEE:本地影像上传到GEE的Assets中,并输入机器学习算法中作为特征变量
    什么是作业指导书sop?sop作业指导书是什么意思?
  • 原文地址:https://blog.csdn.net/Archer__13/article/details/126906948