新西兰服务器

Thymeleaf星号表达式怎么使用


Thymeleaf星号表达式怎么使用

发布时间:2022-01-23 13:16:45 来源:高防服务器网 阅读:90 作者:iii 栏目:web开发

这篇“Thymeleaf星号表达式怎么使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Thymeleaf星号表达式怎么使用”文章吧。

在处理模板时,一般情况都是使用变量表达式 ${…} 来显示变量,还可以使用选定对象表达式 *{…},它也称为星号表达式。
如果在模板中先选定了对象,则需要使用星号表达式。Thymeleaf的内置对象#object效果等同于星号表达式。

开发环境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8

新建一个名称为demo的Spring Boot项目。

1、pom.xml
加入Thymeleaf依赖

<dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-thymeleaf</artifactId>          </dependency>

2、src/main/java/com/example/demo/User.java

package com.example.demo;    public class User {      Integer id;      String name;        public User(Integer id, String name) {          this.id = id;          this.name = name;      }      public Integer getId() {          return id;      }      public void setId(Integer id) {          this.id = id;      }      public String getName() {          return name;      }      public void setName(String name) {          this.name = name;      }  }

3、src/main/java/com/example/demo/TestController.java

package com.example.demo;    import org.springframework.stereotype.Controller;  import org.springframework.ui.Model;  import org.springframework.web.bind.annotation.RequestMapping;    import java.util.ArrayList;  import java.util.List;    @Controller  public class TestController {      @RequestMapping("/")      public String test(Model model){          List<User> users = queryUsers();          model.addAttribute("user", users.get(0));          model.addAttribute("users", users);          return "test";      }        private List<User> queryUsers(){          List<User> users = new ArrayList<User>();          users.add(new User(1,"张三"));          users.add(new User(2,"李四"));          users.add(new User(3,"王五"));          return users;      }  }

4、src/main/resources/templates/test.html 

<!DOCTYPE html>  <html lang="en">  <head>      <meta charset="UTF-8">      <title>Title</title>      <style type="text/css">          table { border-collapse:collapse;}          td { border: 1px solid #C1DAD7;}      </style>  </head>  <body>      <div>使用变量表达式</div>      <table>          <tr>              <td th:text="${user.id}"></td>              <td th:text="${user.name}"></td>          </tr>      </table>        <div>使用星号表达式:使用星号</div>      <table>          <tr th:object="${user}">              <td th:text="*{id}"></td>              <td th:text="*{name}"></td>          </tr>      </table>        <div>使用星号表达式:使用#object对象</div>      <table>          <tr th:object="${user}">              <td th:text="${#object.id}"></td>              <td th:text="${#object.name}"></td>          </tr>      </table>        <div>在循环体中使用星号表达式</div>      <table>          <tr th:each="u : ${users}" th:object="${u}">              <td th:text="*{id}"></td>              <td th:text="*{name}"></td>          </tr>      </table>      </body>  </html>

浏览器访问:http://localhost:8080
显示如下截图所示:

 

以上就是关于“Thymeleaf星号表达式怎么使用”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注高防服务器网行业资讯频道。

[微信提示:高防服务器能助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

[图文来源于网络,不代表本站立场,如有侵权,请联系高防服务器网删除]
[