从列表中根据属性获取对象。

huangapple go评论57阅读模式
英文:

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 ...

&lt;p th:each=&quot;yourObject: ${list}&quot; th:if=&quot;${list.contains(n)}&quot; th:text=&quot;${yourObject.yourProperty}&quot;&gt;Your Object&#39;s property is here only when condition met&lt;/p&gt;

huangapple
  • 本文由 发表于 2020年8月8日 03:54:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63308410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定