如何使用th:each属性迭代模板-原标题:How To Use th:each For Iteration In Thymeleaf Template?

接着来
原文地址:http://www.javabeat.net/use-theach-iteration-thymeleaf-template/
下面是主题:

如何使用th:each属性迭代模板

在之前的文章中,已经说明过了 hello world例子和表达式语言。这个指导文档将说明如何迭代一个list对象中的值。为了迭代,thymeleaf模板提供了th:each属性来在table元素内部迭代一个list对象并展示。这一特性同时也提供了iterationStatus这个上下文对象来获取更多的详细信息,如list的size,row count,或者是first,last元素等等。这一特性对创建一个动态的table非常有用。

  1. 安装应用
    一样的,从hello world上改。
  2. 创建模板
    下面的代码中,包含了th:each属性,它可以被动态的值替换。

employees.xml

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org">
<head>
<title>Iteration in Thymeleaf Template Engine!!</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Employees</h1>
    <table>
      <tr>
        <th>SNO</th>
        <th>ID</th>
        <th>NAME</th>
        <th>CITY</th>
        <th>COUNTRY</th>
      </tr>
      <tr th:each="emp,iterationStatus  : ${employees}">
        <td th:text="${iterationStatus.count}">1</td>
        <td th:text="${emp.id}">001</td>
        <td th:text="${emp.name}">Name</td>
        <td th:text="${emp.city}">City</td>
        <td th:text="${emp.country}">Country</td>
      </tr>
    </table>
</body>
</html>

</body>
</html>

3.创建一个javaBean

Employee.java

package javabeat.net.thymeleaf;

public class Employee {
    private String id;
    private String name;
    private String city;
    private String country;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
}

4.创建servlet

ThymeleafIterationExample.java

package javabeat.net.thymeleaf;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.TreeSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

public class ThymeleafIterationExample extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
        // XHTML is the default mode, but we will set it anyway for better understanding of code
        templateResolver.setTemplateMode("XHTML");
        templateResolver.setPrefix("/WEB-INF/");
        templateResolver.setSuffix(".html");
        templateResolver.setCacheTTLMs(3600000L);
        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
        WebContext ctx = new WebContext(req, resp, getServletConfig().getServletContext(), req.getLocale());
        ArrayList<Employee> employees = new ArrayList<Employee>();
        Employee employee = new Employee();
        employee.setId("001");
        employee.setName("Rahul");
        employee.setCity("Bangalore");
        employee.setCountry("India");
        employees.add(employee);
        employee = new Employee();
        employee.setId("002");
        employee.setName("Deepan");
        employee.setCity("Namakkal");
        employee.setCountry("India");
        employees.add(employee);
        employee = new Employee();
        employee.setId("003");
        employee.setName("Ashwanth");
        employee.setCity("Bangalore");
        employee.setCountry("India");
        employees.add(employee);

        // This will be prefixed with /WEB-INF/ and suffixed with .html
        ctx.setVariable("employees", employees);
        templateEngine.process("employees", ctx, resp.getWriter());

        resp.setContentType("text/html;charset=UTF-8");
        resp.setHeader("Pragma", "no-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setDateHeader("Expires", 1000);
    }
}

5.运行
你会看到下面这个图
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值