Java 如何使Thymeleaf表动态处理多个不同的对象类?
我有一个简单的单页web应用程序,我使用Thymeleaf尝试显示一个表,该表可以处理传递给它的不同对象列表 例如,它应该能够处理Java 如何使Thymeleaf表动态处理多个不同的对象类?,java,spring-boot,user-interface,dynamic,thymeleaf,Java,Spring Boot,User Interface,Dynamic,Thymeleaf,我有一个简单的单页web应用程序,我使用Thymeleaf尝试显示一个表,该表可以处理传递给它的不同对象列表 例如,它应该能够处理列表,其中县看起来像: public class County { String name; String district; LocalDate date; int cases; } public class State { String name; int latitude; int long
列表
,其中县
看起来像:
public class County {
String name;
String district;
LocalDate date;
int cases;
}
public class State {
String name;
int latitude;
int longitude;
LocalDate date;
}
但它还需要能够处理列表
,其中状态
看起来像:
public class County {
String name;
String district;
LocalDate date;
int cases;
}
public class State {
String name;
int latitude;
int longitude;
LocalDate date;
}
到目前为止,我得出的结论是:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<table border="1" cellpadding="10" >
<tr>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
<th>Col 5</th>
</tr>
<tr th:each="county: ${countyList}">
<td th:text="${county.name}"></td>
<td th:text="${county.district}"></td>
<td th:text="${county.date}"></td>
<td th:text="${county.cases}"></td>
</tr>
</table>
</body>
</html>
入门:提供Web内容
第2列
第3列
第4列
第5列
但这只处理具有特定列数的一种类型的对象。我想知道如何动态地生成这个表,使它具有传递给它的对象的每多少个字段,以及如何使这个表处理传递给它的任何对象
这是可以做到的吗?如果不是,我应该如何重新设计它,以便在页面上仍然有一个表
谢谢 您可以使用它。
在模板引擎中发现数据结构既困难又不正确,因此只需使用以下基于目的的块:
<div th:if="${countyList != null}">
<table border="1" cellpadding="10">...</table>
</div>
<div th:if="${stateList != null}">
<table border="1" cellpadding="10">...</table>
</div>
...
...
您可以使用它。
在模板引擎中发现数据结构既困难又不正确,因此只需使用以下基于目的的块:
<div th:if="${countyList != null}">
<table border="1" cellpadding="10">...</table>
</div>
<div th:if="${stateList != null}">
<table border="1" cellpadding="10">...</table>
</div>
...
...
您可能正在查找类似片段的内容。您可能正在查找类似片段的内容。