本文章将介绍Thymeleaf标准表达式语法中的概念。我们将使用th:each标记在模板中迭代产品列表。

编辑源代码以便将产品列表显示为表格行。已经将Product类的对象列表设置为具有变量名称productList的上下文模型(参考:MyController.java中的实现)。

如果要上机实践,请参考:Thymeleaf+SpringMVC5示例项目。这里不再重复创建项目的过程,这里将只介绍如何使用Thymeleaf标准表达式和标签。

这里创建一个Maven Web项目: thymeleaf-tutorials ,其目录结构如下所示 -

1523793460881651.png

数据访问类的实现:DAO.java -

  1. package com.e-learn.dao;
  2.  
  3. import java.math.BigDecimal;
  4. import java.sql.Timestamp;
  5. import java.text.ParseException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.ArrayList;
  8. import java.util.Date;
  9. import java.util.List;
  10.  
  11. import com.e-learn.spring.bean.*;
  12.  
  13. /**
  14. * Mock persistence.
  15. */
  16. public class DAO {
  17.  
  18. private static final String NO_WEBSITE = null;
  19.  
  20. public static Product loadProduct() {
  21. try {
  22. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  23. return new Product("Wooden wardrobe with glass doors", Integer.valueOf(850), sdf.parse("2013-02-18"));
  24. } catch (ParseException ex) {
  25. throw new RuntimeException("Invalid date");
  26. }
  27. }
  28.  
  29. public static List<Product> loadAllProducts() {
  30. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  31. List<Product> products = new ArrayList<Product>();
  32. try {
  33. products.add(new Product("花生油", Integer.valueOf(125), sdf.parse("2018-02-18")));
  34. products.add(new Product("苏打饼干", Integer.valueOf(15), sdf.parse("208-02-15")));
  35. products.add(new Product("拿铁", Integer.valueOf(45), sdf.parse("2019-02-20")));
  36. products.add(new Product("调和油", Integer.valueOf(20), sdf.parse("2019-02-21")));
  37. products.add(new Product("大豆油", Integer.valueOf(49), sdf.parse("2019-02-15")));
  38. products.add(new Product("玉米汁", Integer.valueOf(80), sdf.parse("2019-02-17")));
  39. } catch (ParseException ex) {
  40. throw new RuntimeException("Invalid date");
  41. }
  42. return products;
  43. }
  44.  
  45.  
  46.  
  47. public static Timestamp loadReleaseDate() {
  48. try {
  49. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  50. Date date = sdf.parse("2014-01-31 15:00");
  51. return new Timestamp(date.getTime());
  52. } catch (ParseException ex) {
  53. throw new RuntimeException("Invalid date");
  54. }
  55. }
  56.  
  57. }

控制器类的实现:MyController.java -

  1. package com.e-learn.spring.controller;
  2.  
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.List;
  7.  
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.ui.Model;
  10. import org.springframework.web.bind.annotation.GetMapping;
  11.  
  12. import com.e-learn.dao.DAO;
  13. import com.e-learn.spring.bean.Product;
  14.  
  15. @Controller
  16. public class MyController {
  17.  
  18. @GetMapping("/")
  19. public String index(Model model) throws ParseException {
  20. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  21. Product product = new Product("花生油", 129, sdf.parse("2018-02-18"));
  22. model.addAttribute("product", product);
  23. return "index";
  24. }
  25.  
  26. @GetMapping("/escape")
  27. public String escape(Model model) throws ParseException {
  28. String html = "Welcome to our <b>fantastic</b> grocery store!";
  29. model.addAttribute("html", html);
  30. return "escape";
  31. }
  32.  
  33. @GetMapping("/iteration")
  34. public String iteration(Model model) throws ParseException {
  35. List productList = DAO.loadAllProducts();
  36. model.addAttribute("productList", productList);
  37. return "iteration";
  38. }
  39. }

模板文件的实现:/webapp/WEB-INFO/views/iteration.html -

  1. <html xmlns:th="http://www.thymeleaf.org">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <link rel="stylesheet" th:href="@{/css/main.css}" />
  6. <title>SpringMVC5+Thymeleaf迭代器示例</title>
  7. </head>
  8. <body>
  9. <h2>Spring MVC5 + Thymeleaf迭代器示例</h2>
  10. <table>
  11. <thead>
  12. <tr>
  13. <th>产品名称</th>
  14. <th>价格</th>
  15. <th>有效日期</th>
  16. </tr>
  17. </thead>
  18. <tbody th:remove="all-but-first">
  19. <tr th:each="product : ${productList}">
  20. <td th:text="${product.description}">Red chair</td>
  21. <td th:text="${'¥' + #numbers.formatDecimal(product.price, 1, 2)}">¥350</td>
  22. <td th:text="${#dates.format(product.availableFrom, 'dd-MM-yyyy')}">2018-02-20</td>
  23. </tr>
  24. <tr>
  25. <td>White table</td>
  26. <td>¥200</td>
  27. <td>2018-04-10</td>
  28. </tr>
  29. <tr>
  30. <td>Reb table</td>
  31. <td>¥200</td>
  32. <td>2018-02-20</td>
  33. </tr>
  34. <tr>
  35. <td>Blue table</td>
  36. <td>¥200</td>
  37. <td>2018-02-20</td>
  38. </tr>
  39. </tbody>
  40. </table>
  41.  
  42. </body>
  43. </html>

运行上面项目,在浏览器中显示效果如下 -

1523793463104029.png

如何为每一行添加序号?

重新编写模板代码,模板文件的实现:/webapp/WEB-INFO/views/iteration.html -

  1. <html xmlns:th="http://www.thymeleaf.org">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <link rel="stylesheet" th:href="@{/css/main.css}" />
  6. <title>SpringMVC5+Thymeleaf迭代器示例</title>
  7. </head>
  8. <body>
  9. <h2>Spring MVC5 + Thymeleaf迭代器示例</h2>
  10. <table>
  11. <thead>
  12. <tr>
  13. <th>行序号</th>
  14. <th>产品名称</th>
  15. <th>价格</th>
  16. <th>有效日期</th>
  17. </tr>
  18. </thead>
  19. <tbody th:remove="all-but-first">
  20. <tr th:each="product : ${productList}">
  21. <td th:text="${productStat.count}">1</td>
  22. <td th:text="${product.description}">Red chair</td>
  23. <td th:text="${'¥' + #numbers.formatDecimal(product.price, 1, 2)}">¥350</td>
  24. <td th:text="${#dates.format(product.availableFrom, 'dd-MM-yyyy')}">2018-02-20</td>
  25. </tr>
  26. <tr>
  27. <td>White table</td>
  28. <td>¥200</td>
  29. <td>2018-04-10</td>
  30. </tr>
  31. <tr>
  32. <td>Reb table</td>
  33. <td>¥200</td>
  34. <td>2018-02-20</td>
  35. </tr>
  36. <tr>
  37. <td>Blue table</td>
  38. <td>¥200</td>
  39. <td>2018-02-20</td>
  40. </tr>
  41. </tbody>
  42. </table>
  43.  
  44. </body>
  45. </html>

运行得到结果如下 -

1523793466249989.png