使用th:each迭代数据
通常我们处理的都是一系列的数据,例如文章列表、产品列表,在处理这些数据的时候,一般是把它们一条一条的迭代取出来,thymeleaf提供了th:each属性来处理这方面的数据。
例如,客户购买了多个产品,在购物车中显示产品的时候就需要列出这些产品:
1 2 3 4 5 6 7 8 9 10 11 12 |
<table> <tr> <th>NAME</th> <th>PRICE</th> <th>DESCRIPTION</th> </tr> <tr th:each="prod : ${products}"> <td th:text="${prod.name}">Onions</td> <td th:text="${prod.price}">2.41</td> <td th:text="${prod.description}"></td> </tr> </table> |
Servlet代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); WebContext context = new WebContext(request, response, getServletContext(), request.getLocale()); Product hardDisk = new Product(); hardDisk.setName("hard disk"); hardDisk.setPrice(350F); hardDisk.setDescription("1T"); Product mainBoard = new Product(); mainBoard.setName("Main board"); mainBoard.setPrice(530F); mainBoard.setDescription("Asus"); ArrayList<Product> products = new ArrayList<Product>(); products.add(hardDisk); products.add(mainBoard); context.setVariable("products", products); templateEngine.process("home", context, response.getWriter()); } |
跟踪数据状态
th:each属性在迭代的过程中可以跟踪数据的状态,例如:
- 当前迭代索引,从0开始,使用index属性标识。
- 当前迭代索引,从1开始,使用count标识。
- 数据总量,使用size属性标识。
- 当前迭代变量值,使用current属性标识。
- 当前迭代是奇数还是偶数,布尔值,使用event/odd属性标识。
- 当前值是否第一个迭代值,布尔值,使用first属性标识。
- 当前值是否最后一个迭代值,布尔值,使用last属性标识。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<table> <tr> <th>NAME</th> <th>PRICE</th> <th>DESCRIPTION</th> <th>INDEX</th> <th>COUNT</th> </tr> <tr th:each="prod, iterState : ${products}" th:class="${iterState.odd}?'odd':'even'"> <td th:text="${prod.name}">Onions</td> <td th:text="${prod.price}">2.41</td> <td th:text="${prod.description}"></td> <td th:text="${iterState.index}"></td> <td th:text="${iterState.count}"></td> </tr> </table> |
在这个例子中我们显式定义了一个跟踪变量iterState,如果没有显式定义,那么thymeleaf会自动生成一个,名称为迭代变量+State,例如这里是prodStat。
数据的懒加载
为了减轻数据库的压力,thymeleaf提供了数据懒加载的方式,前提是数据类型必须是ILazyContextVariable
接口的实现,而我们通常是继承自LazyContextVariable
抽象类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); WebContext context = new WebContext(request, response, getServletContext(), request.getLocale()); //实现数据的懒加载 context.setVariable("products", new LazyContextVariable<ArrayList>() { protected ArrayList loadValue() { Product hardDisk = new Product(); hardDisk.setName("hard disk"); hardDisk.setPrice(350F); hardDisk.setDescription("1T"); Product mainBoard = new Product(); mainBoard.setName("Main board"); mainBoard.setPrice(530F); mainBoard.setDescription("Asus"); ArrayList<Product> products = new ArrayList<Product>(); products.add(hardDisk); products.add(mainBoard); return products; } }); templateEngine.process("home", context, response.getWriter()); } |
本地变量
上面代码中迭代过程产生的product变量就是一个本地变量,该变量只能在<tr>及其子范围内被访问。类似的还有前几篇文章介绍的th:with、th:object属性,它们产生的变量也是一个本地变量。
转载请注明:Pure nonsense » thymeleaf数据循环