14.1 订单列表

让我们从创建订单列表页面开始,/WEB-INF/templates/order/list.html

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <title>Good Thymes Virtual Grocery</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6. <link rel="stylesheet" type="text/css" media="all"
  7. href="../../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
  8. </head>
  9. <body>
  10. <h1>Order list</h1>
  11. <table>
  12. <tr>
  13. <th>DATE</th>
  14. <th>CUSTOMER</th>
  15. <th>TOTAL</th>
  16. <th></th>
  17. </tr>
  18. <tr th:each="o : ${orders}" th:class="${oStat.odd}? 'odd'">
  19. <td th:text="${#calendars.format(o.date,'dd/MMM/yyyy')}">13 jan 2011</td>
  20. <td th:text="${o.customer.name}">Frederic Tomato</td>
  21. <td th:text="${#aggregates.sum(o.orderLines.{purchasePrice * amount})}">23.32</td>
  22. <td>
  23. <a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>
  24. </td>
  25. </tr>
  26. </table>
  27. <p>
  28. <a href="../home.html" th:href="@{/}">Return to home</a>
  29. </p>
  30. </body>
  31. </html>

上面的代码应该没有使我们惊讶的东西,除了这一点点OGNL魔法:

  1. <td th:text="${#aggregates.sum(o.orderLines.{purchasePrice * amount})}">23.32</td>

对于订单里的每一个订单行(OrderLine对象),将它的purchasePriceamount属性相乘(通过调用相应的getPurchasePrice()getAmount()方法),并返回数字的列表,之后通过#aggregates.sum(...)函数合计总价。

你已经爱上了❤️OGNL。