【发布时间】:2020-01-25 18:08:03
【问题描述】:
我正在使用 Thymeleaf 来生成 html 文件以用于某些内容共享目的。
下面是html模板
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Testing</title>
</head>
<body>
<table>
<tbody>
<tr>
<td>Message: </td>
<td><p th:text="|${Id}|"> Id</p></td>
<td>< th:text="|${userDetails}|"> </td>
</tr>
</tbody>
</table>
</body>
</html>
下面是我尝试生成不同的html表格或段落的代码
private static final String starttag= "<p><b>$$</b></p>";
private static final String pretag= "<pre<$$</pre>";
public String build(Message model) {
Context context = new Context();
context.setVariable("Id", model.getId());
List<user> users = model.getUsers();
StringBuilder userDetails = new StringBuilder("");
for(user: users) {
String getDepartment = starttag.replace("$$",user.getDepartment());
userInfo = userInfo + pretag.replace("$$", "Name:"+ user.getName());
userDetails.append(userInfo);
}
context.setVariable("userDetails", userDetails);
return templateEngine.process("userTemplate", context);
}
}
但我得到的结果如下
<td><p>123 Id</p></td>
<td> <p><b>Computer Science </b></p> Name:testuser1<pre<</pre></span> </td>
如何在html中添加不同的用户列表
我想要像下面这样
Testuser1
计算机科学
Testuser2
电气
【问题讨论】:
标签: java spring-boot thymeleaf