Spring Boot Web项目全解析:Thymeleaf语法

news/2025/2/3 7:39:05 标签: 前端, spring boot, java

Thymeleaf 语法:

  1. 变量表达式:
    1. 变量表达式即 OGNL 表达式或 Spring EL 表达式(在 Spring 属性中也叫 model attributes)
    2. 如:$(session.user.name)
    3. 示例:<span th:text="${book.author.name}">
  2. 选择(星号)表达式:
    1. 选择表达式很像变量表达式,不过他们用一个预先选择的对象来代替上下问变量容器(map)
    2. 如:*{customer.name}
    3. 被指定的 object 由 th:object 属性定义:
      <div th:object="${book}">
          <span th:text="*{title}">··········</span>
      </div>
  3. 文字国际化表达式:
    1. 文字国际化表达式允许我们从一个外部文件获取区域文字信息(.properties),用 key 索引 Value,还可以提供一组参数(可选)
    2. 如:#{main.title}
  4. URL 表达式:
    1. URL 表达式指的是把一个有用的上下文或回话信息添加到 URL,这个了过程经常被叫做 URL 重写。不需要指定项目名字
    2. 如:@{/hello/index}
    3. URL 还可以设置参数:
      1. @{/hello/details(id=${orserId})}
    4. 示例:
      <form th:action="@{/hello}">
      <a href="main.html" rel="external nofollow" th:href="@{/main}" rel="external">

表达式支持的语法:

  1. 字面:
    1. 文本文字:'one text','Another  one! ',···
    2. 数字文本:1,2,3,4,···
    3. 布尔文本:true,false
    4. 空:null
    5. 文字标记:one,sometext,main,···
  2. 文本操作:
    1. 字符串连接:+
    2. 文本替换:| The  name  is  ${name} |
  3. 算数运算:
    1. 二元运算:+  -  *  /  %
  4. 布尔运算:
    1. 二元运算:and,or
    2. 布尔否定:!,not
  5. 比较和等价:
    1. 比较: <,>,<=,>=
    2. 等值运算符:==,!=
  6. 条件运算符:
    1. if-the:( if) ? (then)
    2. if-then-else:(if) ?(then) :(else)
    3. default:(value) ?:(defaultvalue)
  7. 示例:
    'user is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))
  8. 常用的 thymeleaf 标签:
  9. 标签检测:
    1. 模板:
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd">
          <html lang="en" xmlns:th="http://www.thymeleaf.org">
          <head>
              <title>Title</title>
          </head>
          <body>
          <div  th:text="${hello}" th:id="${hello.toUpperCase()}">xxxx</div>
          <input th:value="${user.getUsername()}">
          <hr>
          <div th:object="${user}">
              <span th:text="*{username}"></span>
      
          </div>
      
      
          <a th:href="" th:if="${user.getAge() == 2}" >年龄</a>
      
          <a  th:class="${user.getAge() > 2}?'class1':'class2'" >年龄</a>
      
          <p th:if="${user.score >= 60 and user.score < 85}">B</p>
          <p th:if="${user.score < 60}">C</p>
          <p th:if="${user.score > 85}">优秀</p>
      
      
          <span th:switch="${user.gender}">
          <p th:case="1">男</p>
          <p th:case="2">女</p>
      </span>
      
      
          <table>
      
              <tr th:each="a,aState:${uList}">
                  <td th:text="${a.username}"></td>
                  <td th:text="${a.password}"></td>
                  <td th:text="${aState.index}"></td>
              </tr>
          </table>
      
          </body>
          </html>
    2. Controller 层:
      java">@Controller
      public class HelloController {
      
          @RequestMapping("/success")
          public String sayHello(HttpServletRequest request, HttpServletResponse response, Model model){
              model.addAttribute("hello","任亮");
              User user = new User();
              user.setPassword("111");
              user.setUsername("renliang");
              user.setAge(11);
              user.setScore(33.0);
              user.setGender(2);
              List<User> list = new ArrayList<>();
              for(int i = 0;i<10;i++){
                  User u = new User();
                  u.setUsername("张三"+1);
                  u.setUsername("111" + i);
                  list.add(u);
              }
      
              model.addAttribute("user",user);
              model.addAttribute("list",list);
              return "success";
      
          }
      }


http://www.niftyadmin.cn/n/5840618.html

相关文章

2 [GitHub遭遇严重供应链投毒攻击]

近日&#xff0c;有黑客针对 Discord Top.gg 的GitHub 账户发起了供应链攻击&#xff0c;此次攻击导致账户密码、凭证和其他敏感信息被盗&#xff0c;同时也影响到了大量开发人员。 Checkmarx 在一份技术报告中提到&#xff0c;黑客在这次攻击中使用了多种TTP&#xff0c;其中…

HTML5 Canvas 与 SVG:让网页图形与动画活跃起来

系列文章目录 01-从零开始学 HTML&#xff1a;构建网页的基本框架与技巧 02-HTML常见文本标签解析&#xff1a;从基础到进阶的全面指南 03-HTML从入门到精通&#xff1a;链接与图像标签全解析 04-HTML 列表标签全解析&#xff1a;无序与有序列表的深度应用 05-HTML表格标签全面…

13 尺寸结构模块(size.rs)

一、size.rs源码 // Copyright 2013 The Servo Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE…

Java/Kotlin HashMap 等集合引发 ConcurrentModificationException

在对一些非并发集合同时进行读写的时候&#xff0c;会抛出 ConcurrentModificationException 异常产生示例 示例一&#xff08;单线程&#xff09;&#xff1a; 遍历集合时候去修改 抛出 ConcurrentModificationException 的主要原因是当你在遍历一个集合&#xff08;如 Map…

MVC 文件夹:架构之美与实际应用

MVC 文件夹:架构之美与实际应用 引言 MVC(Model-View-Controller)是一种设计模式,它将应用程序分为三个核心组件:模型(Model)、视图(View)和控制器(Controller)。这种架构模式不仅提高了代码的可维护性和可扩展性,而且使得开发流程更加清晰。本文将深入探讨MVC文…

UE学习日志#19 C++笔记#5 基础复习5 引用1

C中的引用&#xff08;reference&#xff09;是另一个变量的别名。对引用的所有修改都会更改其引用的变量的值。可以将引用视为隐式指针&#xff0c;它省去了获取变量地址和解引用指针的麻烦。另外&#xff0c;可以将引用视为原始变量的另一个名称。可以创建独立的引用变量&…

2.[网鼎杯 2020 朱雀组]phpweb

打开题目页面如下&#xff0c;发现刷新后时间回显也随之刷新改变 用burp suite抓包&#xff0c;发送到重放器查看 看到有传参&#xff0c;看到返回结果"Y-m-dh:i:sa" 在 PHP 里&#xff0c;date() 函数的作用是格式化本地日期和时间&#xff0c;然后返回格式化后的字…

Hot100之哈希

1两数之和 题目 思路解析 解法1--两次循环 解法2--哈希表一次循环 代码 解法1--两次循环 class Solution {public int[] twoSum(int[] nums, int target) {int nums1[] new int[2];int length nums.length;for (int i 0; i < length; i) {for (int j i 1; j < …