英文:
Getting object from list by attribute
问题
我是新手使用Spring Boot,我在这个问题上卡住了。
我有一个对象列表,我想检查其中一个属性是否等于另一个变量,如果是的话,我想获取该对象。我只能做的是检查列表是否包含具有该属性的对象。我无法检索到实际的对象。
不确定从这里如何继续。
I'm new with spring boot and I'm stuck on this. I have a list of objects and I want to check if one of its attributes are equal to another variable and if it does, I want to get that object. All I can do is checking to see if the list contains an object with that attribute. I cannot retrieve the actual object. Not sure how to move on from here英文:
<p th:if="${list.contains(n)}"></p>
答案1
得分: 1
这涉及到Thymeleaf的属性优先级。对于你的列表,你可以使用 th:each
循环遍历对象,并使用 th:if
条件ally地过滤列表中的当前迭代对象。只要条件匹配,你就可以访问它的属性。因此,你的代码可能如下所示...
<p th:each="yourObject: ${list}" th:if="${list.contains(n)}" th:text="${yourObject.yourProperty}">Your Object的属性仅在条件满足时出现在这里</p>
英文:
It's all about Thymeleaf Attribute Precedence. For your list, you would use th:each
to loop through objects and th:if
to conditionally filter the current iteration object from the list. As long as condition match, you've got your object and able to access it's properties. So your code may look like ...
<p th:each="yourObject: ${list}" th:if="${list.contains(n)}" th:text="${yourObject.yourProperty}">Your Object's property is here only when condition met</p>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论