本文章将介绍Thymeleaf标准表达式语法中的概念。我们将使用th:if
和th:unless
标记在模板中迭代显示产品列表,如果产品的价格大于100,则会显示:“特殊提供”。
编辑源代码以便将产品列表显示为表格行。已经将Product
类的对象列表设置为具有变量名称productList
的上下文模型(参考:MyController.java
中的实现)。
如果要上机实践,请参考:Thymeleaf+SpringMVC5示例项目。这里不再重复创建项目的过程,这里将只介绍如何使用Thymeleaf
标准表达式和标签。
这里创建一个Maven Web项目: thymeleaf-tutorials ,其目录结构如下所示 -
数据访问类的实现:DAO.java -
package com.voidme.dao;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.voidme.spring.bean.*;
/**
* Mock persistence.
*/
public class DAO {
private static final String NO_WEBSITE = null;
public static Product loadProduct() {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return new Product("Wooden wardrobe with glass doors", Integer.valueOf(850), sdf.parse("2013-02-18"));
} catch (ParseException ex) {
throw new RuntimeException("Invalid date");
}
}
public static List<Product> loadAllProducts() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<Product> products = new ArrayList<Product>();
try {
products.add(new Product("花生油", Integer.valueOf(125), sdf.parse("2018-02-18")));
products.add(new Product("苏打饼干", Integer.valueOf(15), sdf.parse("208-02-15")));
products.add(new Product("拿铁", Integer.valueOf(45), sdf.parse("2019-02-20")));
products.add(new Product("调和油", Integer.valueOf(20), sdf.parse("2019-02-21")));
products.add(new Product("大豆油", Integer.valueOf(49), sdf.parse("2019-02-15")));
products.add(new Product("玉米汁", Integer.valueOf(80), sdf.parse("2019-02-17")));
} catch (ParseException ex) {
throw new RuntimeException("Invalid date");
}
return products;
}
public static Timestamp loadReleaseDate() {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = sdf.parse("2014-01-31 15:00");
return new Timestamp(date.getTime());
} catch (ParseException ex) {
throw new RuntimeException("Invalid date");
}
}
}
控制器类的实现:MyController.java -
package com.voidme.spring.controller;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import com.voidme.dao.DAO;
import com.voidme.spring.bean.Product;
@Controller
public class MyController {
@GetMapping("/")
public String index(Model model) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Product product = new Product("花生油", 129, sdf.parse("2018-02-18"));
model.addAttribute("product", product);
return "index";
}
@GetMapping("/escape")
public String escape(Model model) throws ParseException {
String html = "Welcome to our <b>fantastic</b> grocery store!";
model.addAttribute("html", html);
return "escape";
}
@GetMapping("/iteration")
public String iteration(Model model) throws ParseException {
List productList = DAO.loadAllProducts();
model.addAttribute("productList", productList);
return "iteration";
}
@GetMapping("/conditional")
public String conditional(Model model) throws ParseException {
List productList = DAO.loadAllProducts();
model.addAttribute("productList", productList);
return "conditional";
}
}
模板文件的实现:/webapp/WEB-INFO/views/conditional.html -
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" th:href="@{/css/main.css}" />
<style>
span.offer {
font-weight: bold;
color: green;
}
</style>
<title>SpringMVC5+Thymeleaf条件示例</title>
</head>
<body>
<h2>Spring MVC5 + Thymeleaf条件示例</h2>
<table>
<thead>
<tr>
<th>行序号</th>
<th>产品名称</th>
<th>价格</th>
<th>有效日期</th>
<th>备注</th>
</tr>
</thead>
<tbody th:remove="all-but-first">
<tr th:each="product : ${productList}">
<td th:text="${productStat.count}">1</td>
<td th:text="${product.description}">Red chair</td>
<td th:text="${'¥' + #numbers.formatDecimal(product.price, 1, 2)}">¥350</td>
<td th:text="${#dates.format(product.availableFrom, 'dd-MM-yyyy')}">2018-02-20</td>
<td><span th:if="${product.price gt 100}" class="offer">特殊提供</span></td>
</tr>
<tr>
<td>White table</td>
<td>¥200</td>
<td>2018-04-10</td>
<td> </td>
</tr>
<tr>
<td>Reb table</td>
<td>¥200</td>
<td>2018-02-20</td>
<td> </td>
</tr>
<tr>
<td>Blue table</td>
<td>¥200</td>
<td>2018-02-20</td>
<td> </td>
</tr>
</tbody>
</table>
</body>
</html>
运行上面项目,在浏览器中显示效果如下 -
高级应用
在这部分中,我们将演示其它几个标签(如:th:switch
,th:case
和th:unless
)的用法。
需要重写以下几个文件,DAO.java -
package com.voidme.dao;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.voidme.spring.bean.*;
/**
* Mock persistence.
*/
public class DAO {
private static final String NO_WEBSITE = null;
public static Product loadProduct() {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return new Product("Wooden wardrobe with glass doors", Integer.valueOf(850), sdf.parse("2013-02-18"), "");
} catch (ParseException ex) {
throw new RuntimeException("Invalid date");
}
}
public static List<Product> loadAllProducts() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<Product> products = new ArrayList<Product>();
try {
products.add(new Product("花生油", Integer.valueOf(125), sdf.parse("2018-02-18"),"CG"));
products.add(new Product("苏打饼干", Integer.valueOf(15), sdf.parse("208-02-15"),"CG"));
products.add(new Product("拿铁", Integer.valueOf(45), sdf.parse("2019-02-20"),"PT"));
products.add(new Product("调和油", Integer.valueOf(20), sdf.parse("2019-02-21"),"CX"));
products.add(new Product("大豆油", Integer.valueOf(49), sdf.parse("2019-02-15"),""));
products.add(new Product("玉米汁", Integer.valueOf(80), sdf.parse("2019-02-17"),""));
} catch (ParseException ex) {
throw new RuntimeException("Invalid date");
}
return products;
}
public static Timestamp loadReleaseDate() {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = sdf.parse("2014-01-31 15:00");
return new Timestamp(date.getTime());
} catch (ParseException ex) {
throw new RuntimeException("Invalid date");
}
}
}
产品类的实现:Product.java -
package com.voidme.spring.bean;
import java.util.Date;
public class Product {
private String description;
private Integer price;
private Date availableFrom;
private String saleType;// 销售类型:促销,拼团,闪购,其它
public String getSaleType() {
return saleType;
}
public void setSaleType(String saleType) {
this.saleType = saleType;
}
public Product(final String description, final Integer price, final Date availableFrom, final String saleType) {
this.description = description;
this.price = price;
this.availableFrom = availableFrom;
this.saleType = saleType;
}
public Date getAvailableFrom() {
return this.availableFrom;
}
public void setAvailableFrom(final Date availableFrom) {
this.availableFrom = availableFrom;
}
public String getDescription() {
return this.description;
}
public void setDescription(final String description) {
this.description = description;
}
public Integer getPrice() {
return this.price;
}
public void setPrice(final Integer price) {
this.price = price;
}
}
控制器类的实现:MyController.java -
package com.voidme.spring.controller;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import com.voidme.dao.DAO;
import com.voidme.spring.bean.Product;
@Controller
public class MyController {
@GetMapping("/")
public String index(Model model) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Product product = new Product("花生油", 129, sdf.parse("2018-02-18"), "");
model.addAttribute("product", product);
return "index";
}
@GetMapping("/escape")
public String escape(Model model) throws ParseException {
String html = "Welcome to our <b>fantastic</b> grocery store!";
model.addAttribute("html", html);
return "escape";
}
@GetMapping("/iteration")
public String iteration(Model model) throws ParseException {
List productList = DAO.loadAllProducts();
model.addAttribute("productList", productList);
return "iteration";
}
@GetMapping("/conditional")
public String conditional(Model model) throws ParseException {
List productList = DAO.loadAllProducts();
model.addAttribute("productList", productList);
return "conditional";
}
}
模板文件的实现:/webapp/WEB-INFO/views/conditional.html -
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" th:href="@{/css/main.css}" />
<style>
span.offer {
font-weight: bold;
color: green;
}
</style>
<title>SpringMVC5+Thymeleaf条件示例</title>
</head>
<body>
<h2>Spring MVC5 + Thymeleaf条件示例</h2>
<table>
<thead>
<tr>
<th>行序号</th>
<th>产品名称</th>
<th>价格</th>
<th>有效日期</th>
<th>销售类型</th>
<th>备注</th>
</tr>
</thead>
<tbody th:remove="all-but-first">
<tr th:each="product : ${productList}">
<td th:text="${productStat.count}">1</td>
<td th:text="${product.description}">Red chair</td>
<td th:text="${'¥' + #numbers.formatDecimal(product.price, 1, 2)}">¥350</td>
<td th:text="${#dates.format(product.availableFrom, 'dd-MM-yyyy')}">2018-02-20</td>
<td th:switch="${product.saleType}">
<span th:case="'CG'">闪购</span>
<span th:case="'PT'">拼团</span>
<span th:case="'CX'">促销</span>
<span th:case="*">其它</span>
</td>
<td><span th:class="${product.price gt 100}?'offer'" th:text="${product.price}">特殊提供</span></td>
</tr>
<tr>
<td>White table</td>
<td>¥200</td>
<td>2018-04-10</td>
<td> </td>
</tr>
<tr>
<td>Reb table</td>
<td>¥200</td>
<td>2018-02-20</td>
<td> </td>
</tr>
<tr>
<td>Blue table</td>
<td>¥200</td>
<td>2018-02-20</td>
<td> </td>
</tr>
</tbody>
</table>
</body>
</html>
执行上面示例代码,得到以下结果 -