Thymeleafチュートリアル:第14章食料品のもう少しページ

目次



食料品の14ページ



これで、Thymeleafの使用について多くのことを理解し、注文管理のためにサイトに新しいページを追加できます。



HTMLコードに焦点を当てますが、対応するコントローラーを確認したい場合は、ソースコードを確認できます。



14.1注文リスト



注文リストページ/WEB-INF/templates/order/list.htmlを作成することから始めましょう。



<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Good Thymes Virtual Grocery</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" type="text/css" media="all" href="../../../css/gtvg.css" th:href="@{/css/gtvg.css}" /> </head> <body> <h1>Order list</h1> <table> <tr> <th>DATE</th> <th>CUSTOMER</th> <th>TOTAL</th> <th></th> </tr> <tr th:each="o : ${orders}" th:class="${oStat.odd}? 'odd'"> <td th:text="${#calendars.format(o.date,'dd/MMM/yyyy')}">13 jan 2011</td> <td th:text="${o.customer.name}">Frederic Tomato</td> <td th:text="${#aggregates.sum(o.orderLines.{purchasePrice * amount})}">23.32</td> <td> <a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a> </td> </tr> </table> <p> <a href="../home.html" th:href="@{/}">Return to home</a> </p> </body> </html>
      
      





ここには、この小さなOGNLマジック以外に私たちを驚かせるようなものは何もありません。



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





各オーダーライン(OrderLineオブジェクト)について、purchasePrice、amount、summaryのプロパティを乗算し(対応するメソッドgetPurchasePrice()およびgetAmount()を呼び出して)、結果を数値のリストに返し、後で集約関数#aggregates.sum(...)を取得して注文値。



あなたはOGNLの力を愛さなければなりません。



14.2注文の詳細



次に注文の詳細ページで、アスタリスク構文を多用します。



 <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Good Thymes Virtual Grocery</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" type="text/css" media="all" href="../../../css/gtvg.css" th:href="@{/css/gtvg.css}" /> </head> <body th:object="${order}"> <h1>Order details</h1> <div> <p><b>Code:</b> <span th:text="*{id}">99</span></p> <p> <b>Date:</b> <span th:text="*{#calendars.format(date,'dd MMM yyyy')}">13 jan 2011</span> </p> </div> <h2>Customer</h2> <div th:object="*{customer}"> <p><b>Name:</b> <span th:text="*{name}">Frederic Tomato</span></p> <p> <b>Since:</b> <span th:text="*{#calendars.format(customerSince,'dd MMM yyyy')}">1 jan 2011</span> </p> </div> <h2>Products</h2> <table> <tr> <th>PRODUCT</th> <th>AMOUNT</th> <th>PURCHASE PRICE</th> </tr> <tr th:each="ol,row : *{orderLines}" th:class="${row.odd}? 'odd'"> <td th:text="${ol.product.name}">Strawberries</td> <td th:text="${ol.amount}" class="number">3</td> <td th:text="${ol.purchasePrice}" class="number">23.32</td> </tr> </table> <div> <b>TOTAL:</b> <span th:text="*{#aggregates.sum(orderLines.{purchasePrice * amount})}">35.23</span> </div> <p> <a href="list.html" th:href="@{/order/list}">Return to order list</a> </p> </body> </html>
      
      





このネストされたオブジェクトの選択に加えて、ここではそれほど新しいものはありません。



 <body th:object="${order}"> <div th:object="*{customer}"> <p><b>Name:</b> <span th:text="*{name}">Frederic Tomato</span></p> </div> </body>
      
      





...これにより、この* {name}と同等になります。



 <p><b>Name:</b> <span th:text="${order.customer.name}">Frederic Tomato</span></p>
      
      






All Articles