• Thymeleaf语法详解


    目录

    一、Thymeleaf介绍

    (1)依赖

    (2)视图

    (3)控制层

    二、变量输出

    三、操作字符串

    四、操作时间

    五、条件判断

    六、遍历集合

    (1)迭代遍历

    (2)将遍历的状态变量封装到一个对象中

    七、遍历Map

    八、获取域中的数据

    (1)控制层

    (2)视图

    九、Thymeleaf中的URL写法


    一、Thymeleaf介绍

    Thymeleaf是一款用于渲染XML/HTML5内容的模板引擎,类似JSP。它可以轻易的与SpringMVC等Web框架进行集成作为Web应用的模板引擎。在SpringBoot中推荐使用Thymeleaf编写动态页面。

    Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用。

    Thymeleaf在有网络和无网络的环境下皆可运行,它即可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。没有数据时,Thymeleaf的模板可以静态地运行;当有数据返回到页面时,Thymeleaf标签会动态地替换掉静态内容,使页面动态显示。

    (1)依赖

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-thymeleafartifactId>
    4. dependency>
    5. <dependency>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-webartifactId>
    8. dependency>

    (2)视图

    一定要注意templates的html文件不能直接访问,需要编写controller跳转到页面中

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>thymeleaf入门title>
    6. head>
    7. <body>
    8. <h2 th:text="${name}">h2>
    9. body>
    10. html>

    (3)控制层

    1. @Controller
    2. public class PageController {
    3. // 页面跳转
    4. @GetMapping("/show")
    5. public String showPage(Model model){
    6. model.addAttribute("name","Hello Thymeleaf");
    7. return "index";
    8. }
    9. }

    二、变量输出

    th:text

    作用:将model的值作为内容放入标签中。

    th:value

    作用:将model的值放入input标签的value属性中

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>thymeleaf入门title>
    6. head>
    7. <body>
    8. <h1>name=<span th:text="${name}">span>h1>
    9. <input th:value="${name}">
    10. body>
    11. html>

    三、操作字符串

    Thymeleaf提供了一些内置对象可以操作数据,内置对象可直接在模板中使用,这些对象是以#引用的,操作字符串的内置对象为strings。

    1. 方法 说明
    2. ${#strings.isEmpty(key)} 判断字符串是否为空,如果为空返回true,否则返回false
    3. ${#strings.contains(msg,'T')} 判断字符串是否包含指定的子串,如果包含返回true,否则返回false
    4. ${#strings.startsWith(msg,'a')} 判断当前字符串是否以子串开头,如果是返回true,否则返回false
    5. ${#strings.endsWith(msg,'a')} 判断当前字符串是否以子串结尾,如果是返回true,否则返回false
    6. ${#strings.length(msg)} 返回字符串的长度
    7. ${#strings.indexOf(msg,'h')} 查找子串的位置,并返回该子串的下标,如果没找到则返回-1
    8. ${#strings.substring(msg,2,5)} 截取子串,用法与JDK的subString方法相同
    9. ${#strings.toUpperCase(msg)} 字符串转大写
    10. ${#strings.toLowerCase(msg)} 字符串转小写
    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>thymeleaf入门title>
    6. head>
    7. <body>
    8. <span th:text="${#strings.length(name)}">span>
    9. <span th:text="${#strings.startsWith(name,'n')}">span>
    10. body>
    11. html>

    四、操作时间

    操作时间的内置对象为dates

    1. 方法 说明
    2. ${#dates.format(key)} 格式化日期,默认的以浏览器默认语言为格式化标准
    3. ${#dates.format(key,'yyyy/MM/dd')} 按照自定义的格式做日期转换
    4. ${#dates.year(key)} 取年
    5. ${#dates.month(key)} 取月
    6. ${#dates.day(key)} 取日
    1. 添加数据
    2. model.addAttribute("date",new Date(130,01,01));
    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>thymeleaf入门title>
    6. head>
    7. <body>
    8. <span th:text="${#dates.format(date)}">span>
    9. <span th:text="${#dates.format(date,'yyyy/MM/dd')}">span>
    10. <span th:text="${#dates.year(date)}">span>
    11. <span th:text="${#dates.month(date)}">span>
    12. body>
    13. html>

    五、条件判断

    th:if  用于条件判断

    th:switch/th:case  th:switch/th:case与Java中的switch语句等效。th:case="*"表示Java中switch的default,即没有case的值为true时显示th:case="*"的内容。

    1. 添加数据
    2. model.addAttribute("age",3);
    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>thymeleaf入门title>
    6. head>
    7. <body>
    8. <span th:if="${age}==1">我是1span>
    9. <span th:if="${age}==2">我是2span>
    10. <span th:if="${age}==3">我是3span>
    11. body>
    12. html>
    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>thymeleaf入门title>
    6. head>
    7. <body>
    8. <div th:switch="${age}">
    9. <span th:case="1">我是1span>
    10. <span th:case="2">我是2span>
    11. <span th:case="3">我是3span>
    12. <span th:case="4">我是4span>
    13. <span th:case="*">我是不知道span>
    14. div>
    15. body>
    16. html>

    六、遍历集合

    (1)迭代遍历

    th:each  迭代器,用于循环迭代集合

    1. @GetMapping("/c1")
    2. public String t1(Model model){
    3. List users=new ArrayList<>();
    4. Users users1=new Users("2","pet",54);
    5. Users users2=new Users("18","ioi",7);
    6. Users users3=new Users("223","ppp",65);
    7. users.add(users1);
    8. users.add(users2);
    9. users.add(users3);
    10. model.addAttribute("li",users);
    11. return "index";
    12. }
    1. <body>
    2. <table border="1" width="50%">
    3. <tr>
    4. <th>idth>
    5. <th>nameth>
    6. <th>ageth>
    7. tr>
    8. <tr th:each="u:${li}">
    9. <td th:text="${u.id}">td>
    10. <td th:text="${u.name}">td>
    11. <td th:text="${u.age}">td>
    12. tr>
    13. table>
    14. body>

    (2)将遍历的状态变量封装到一个对象中

    thymeleaf将遍历的状态变量封装到一个对象中,通过该对象的属性可以获取状态变量:

    状态变量含义
    index当前迭代器的索引,从0开始
    count当前迭代对象的计数,从1开始
    size被迭代对象的长度
    odd/even布尔值,当前循环是否是偶数/奇数,从0开始
    first布尔值,当前循环的是否是第一条,如果是返回true,否则返回false
    last布尔值,当前循环的是否是最后一条,如果是则返回true,否则返回false
    1. <tr th:each="user,status : ${li}">
    2. <td th:text="${user.id}">td>
    3. <td th:text="${user.name}">td>
    4. <td th:text="${user.age}">td>
    5. <td th:text="${status.index}">td>
    6. <td th:text="${status.count}">td>
    7. <td th:text="${status.size}">td>
    8. <td th:text="${status.odd}">td>
    9. <td th:text="${status.even}">td>
    10. <td th:text="${status.first}">td>
    11. <td th:text="${status.last}">td>
    12. tr>

    七、遍历Map

    遍历Map出来的每一项是键值对,key获取键,value获取值

    1. @GetMapping("/c1")
    2. public String t1(Model model){
    3. Map map=new HashMap<>();
    4. map.put("u1",new Users("16","张三",90));
    5. map.put("u2",new Users("90","李四",12));
    6. map.put("u4",new Users("1","王一",16));
    7. model.addAttribute("us",map);
    8. return "index";
    9. }
    1. <body>
    2. <table border="1" width="50%">
    3. <tr>
    4. <th>th>
    5. <th>idth>
    6. <th>nameth>
    7. <th>ageth>
    8. tr>
    9. <tr th:each="u:${us}">
    10. <th th:text="${u.key}">th>
    11. <th th:text="${u.value.id}">th>
    12. <th th:text="${u.value.name}">th>
    13. <th th:text="${u.value.age}">th>
    14. tr>
    15. table>
    16. body>

    八、获取域中的数据

    (1)控制层

    1. @GetMapping("/c1")
    2. public String t1(HttpServletRequest request, HttpSession session){
    3. request.setAttribute("q1","我是request数据");
    4. session.setAttribute("s1","我绝对是session");
    5. ServletContext servletContext=session.getServletContext();
    6. servletContext.setAttribute("c1","我真的是context'数据");
    7. return "index";
    8. }

    (2)视图

    1. <body>
    2. request1=<span th:text="${#request.getAttribute('q1')}">span>
    3. request2=<span th:text="${#httpServletRequest.getAttribute('q1')}">span>
    4. session=<span th:text="${session.s1}">span>
    5. session1=<span th:text="${#httpSession.getAttribute('s1')}">span>
    6. context1=<span th:text="${application.c1}">span>
    7. context2=<span th:text="${#servletContext.getAttribute('c1')}">span>
    8. body>

    九、Thymeleaf中的URL写法

    在Thymeleaf中路径的写法为@{路径}

    1. <a th:href="@{show2?id=1&name=gq}">静态参数一a>
    2. <a th:href="@{show2(id=2,name=gq)}">静态参数二a>
    3. <a th:href="@{'show2?id='+${id}+'&name='+${name}}">动态参数一a>
    4. <a th:href="@{show2(id=${id},name=${name})}">动态参数二a>

  • 相关阅读:
    读书·计算机组成与设计:软硬件接口RISC-V版·第三章
    golang POST data 解析
    MVC架构模式实现银行转账
    蓝桥杯备战15.完全二叉树的权值
    基于sklearn的决策树应用实战
    Java基于JSP+Servlet的校友论坛管理系统
    【Unity】流体模拟(更新ing)
    unidbg-补文件环境(二)
    Java错误:找不到或无法加载主类
    paddlepaddle(一)文字识别
  • 原文地址:https://blog.csdn.net/gaoqiandr/article/details/132926602