接着来
原文地址: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非常有用。
- 安装应用
一样的,从hello world上改。 - 创建模板
下面的代码中,包含了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.运行
你会看到下面这个图