[英]spring boot / thymeleaf nested loop object access
I am using Spring Boot and Thymeleaf to create a single landing page for my application. 我正在使用Spring Boot和Thymeleaf为我的应用程序创建一个登陆页面。 For this, I need to render a List of Host objects that all contain a Container. 为此,我需要呈现一个都包含一个Container的Host对象的List。 Here is the relevant code: 以下是相关代码:
public class Container {
private String name;
private String baseUrl;
private String status;
public Container(String name, String baseUrl, String status) {
this.name = name;
this.baseUrl = baseUrl;
this.status = status;
}
public String getName() { return name; }
public String getBaseUrl() { return baseUrl; }
public String getStatus() { return status; }
}
public class Host {
private HashMap<String, Container> containers;
....
public List<Container> getContainers() {
return containers.values();
}
}
@RequestMapping("/")
public class IndexController {
@RequestMapping("/")
public String getIndex(Model model) {
model.addAttribute("hosts", hostRepository.getAllServers());
return "index";
}
}
Now I want to iterate over all servers and display the information about each Container in a table. 现在,我要遍历所有服务器,并在表中显示有关每个容器的信息。 My Thymeleaf template looks like this: 我的Thymeleaf模板如下所示:
<div class="panel panel-default" th:each="host : ${hosts}">
<div class="panel-heading">
<b th:text="${host.name}">Host X</b>
<div class="panel-body">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>URL</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr th:each="container : ${host.getContainers()}">
<!-- HERE IS THE PROBLEM -->
<td th:text="${container.name}">Service1</td>
<td th:text="${container.baseUrl}">domain.com/api/url</td>
<td th:text="${container.status}">RUNNING</td>
<!-- HERE ENDS THE PROBLEM -->
</tr>
</tbody>
</table>
</div>
</div>
</div>
My problem is the part where is access the container's properties (marked by the commentary). 我的问题是要访问容器的属性(由注释标记)的部分。 Every time I get a SpringEL Exception. 每次我得到一个SpringEL Exception。 If I remove the th:text="${container.xy}"
and replaces it with th:text="${container}
a String version of the container is shown so I have access to the object and the loop it working properly. I also tried to replace the field access with getters (eg getStatus()
) but it also does not work. 如果删除th:text="${container.xy}"
并替换为th:text="${container}
,则会显示该容器的字符串版本,因此我可以访问该对象,并使该循环正常运行我还尝试用getter(例如getStatus()
)替换字段访问,但是它也不起作用。
Thanks for your help. 谢谢你的帮助。 If you need more information, feel free to ask. 如果您需要更多信息,请随时询问。
Setup: 设定:
edit : The exception thrown is: nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "container.name" (index:35)
where index:35 is the first problematic line. 编辑 :引发的异常是: nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "container.name" (index:35)
,其中index:35是有问题的第一行。
The toString() output when using ${container}
is jenkins=com.my.app.Container@7552c269
and jenkins is the name
attribute of the Container instance. 使用${container}
时,toString()的输出为jenkins=com.my.app.Container@7552c269
而jenkins是Container实例的name
属性。
Solution It seemed that the nested loop was iterating over a Map instead of a List. 解决方案似乎嵌套循环是在Map而不是List上进行迭代。 Changing ${container.xy}
to ${container.getValue().xy}
solved the problem. 将${container.xy}
更改${container.xy}
${container.getValue().xy}
可解决此问题。
Solution 解
It seemes that the nested loop was iterating over a org.thymeleaf.util.EvaluationUtil$MapEntry
instead of a List. 似乎嵌套循环正在org.thymeleaf.util.EvaluationUtil$MapEntry
而不是List上进行迭代。 Changing ${container.xy}
to ${container.getValue().xy}
solved the problem. 将${container.xy}
更改${container.xy}
${container.getValue().xy}
可解决此问题。
Bits learned along the way: 一路学到的东西:
toString()
method to obtain formatted information about the object iterating over. 重写toString()
方法以获得有关迭代对象的格式化信息。 In this case the output was key=value
which altough value
was expected. 在这种情况下,输出为key=value
,这是预期的全部value
。 This gave a hint that the current object must be something else than a Container
instance 这提示当前对象必须是Container
实例以外的其他对象 getClass()
on the current object during the iteration to check if something went wrong here 在迭代期间在当前对象上使用getClass()
来检查这里是否出错
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.