简体   繁体   中英

Getting object from list by attribute

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM