How to print Array size in thymeleaf?
Asked Answered
M

3

27

I'm using Thymeleaf with spring mvc 4 but I have a problem when I want to print a list size

<tr th:each="u : ${users}" th:id="${u.username}" class="odd gradeX">
    <th></th>
    <td th:text="${u.username}"></td>
    <td th:text="${u.email}"></td>
    <td th:switch="${u.enabled}">
        <span th:case="true" class="badge badge-primary">Acitf</span>
        <span th:case="false" class="badge badge-danger">Désactivée</span>
        <span th:case="*" class="badge badge-dark">Inconnue</span>
    </td>
    <td th:switch="${u.roles}">
        <span th:case="ROLE_SUPER_ADMIN">Super ADmin</span>
        <span th:case="ROLE_MANGER">Manager</span>
        <span th:case="ROLE_SUPERVISEUR">Superviseur</span>
        <span th:case="ROLE_USER">User</span>
        <span th:case="*">Inconnue</span>

    </td>
    <td th:text="${#calendars.format(u.creationDate,'dd/MM/yyyy hh:mm')}"></td>
    <td th:text="${#calendars.format(u.lastLogin,'dd/MM/yyyy hh:mm')}"></td>
    **
    <td th:text="${u.engines}"></td>
    <td th:text="${u.subordonnes}"></td>
    **
    <td></td>
</tr>

the problem is here th:text="${u.engines}</td>. engines is an ArrayList in my User entity. I tried th:size and th:list but it didn't work.

Can anyone help me please

Edited

here is my User entity:

@OneToMany(mappedBy = "superieur")
@JsonIgnore
private List<User> subordonnes = new ArrayList<User>();

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "USERS_ENGINES", joinColumns = @JoinColumn(name = "USER_ID"), inverseJoinColumns = @JoinColumn(name = "NUM_EQUIPMENT"))
@JsonIgnore
private List<Engine> engines = new ArrayList<Engine>();

and my Controller method:

@RequestMapping(value = {"/listeUsers"})
public ModelAndView userlistePage() {
    ModelAndView model = new ModelAndView();
    User logedUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    
    List<User> users = new ArrayList<User>();
    users = userService.findUserSub(logedUser);
    model.addObject("users", users);
    model.setViewName("utilisateur/list_users");
    return model;
}
Monaghan answered 19/4, 2017 at 0:6 Comment(6)
Did u.engines.size() not work?Shepperd
no it didn't work eitherMonaghan
Can you add your controller/context and model code?Infestation
@Infestation here is my controller code and where i declared the listsMonaghan
What do you mean it didn't work? Did you get an error? If you attach a debugger or log the engines object, what does it say is inside? Does it have a size?Infestation
u.engines.size() really should have worked; can you try it again and update the code to reflect what you tried?Infestation
S
65

Try using the utility method for org.thymeleaf.expression.Lists:

<td th:text="${#lists.size(u.engines)}">[Engine Size]</td>
Spy answered 19/4, 2017 at 3:34 Comment(3)
how to get size of inner objects?Ransell
Did you try u.engines.someInnerObjectName ?Spy
Docs links for reference are here: thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#lists and here: thymeleaf.org/apidocs/thymeleaf/3.0.13.RELEASE/org/thymeleaf/…Merovingian
F
0

Besides the util method vphilipnyc described, instead of list.size, use list.size().

When writing list.size, it's trying to call list.getSize() and an List (or a collection) doesn't have a getter named like this.

Ferromagnetism answered 27/10, 2020 at 8:7 Comment(0)
K
0

The easiest thing is to make use of length property

Example Snippet written below

<p id="clientCountTracker" style="display: block"
                        th:text="${contentDTO.clients.length}"
                        ></p>

Where "client" is an array

Keloid answered 11/4, 2022 at 12:30 Comment(1)
OP has a List thoughSpy

© 2022 - 2024 — McMap. All rights reserved.