Java 如何在百里香叶中打印数组大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43484142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:55:06  来源:igfitidea点击:

How to print Array size in thymeleaf?

javaspring-mvcthymeleaf

提问by Wafdi Ahmed

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

我正在将Thymeleafspring mvc 4一起使用,但是当我想打印列表大小时遇到​​问题

<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>. enginesis an ArrayList in my Userentity. I tried th:sizeand th:listbut it didn't work.

问题就在这里th:text="${u.engines}</td>engines是我User实体中的一个 ArrayList 。我试过了th:sizeth:list但没有用。

Can anyone help me please

任何人都可以帮助我吗

Edited

已编辑

here is my Userentity:

这是我的User实体:

@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;
}

采纳答案by vphilipnyc

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

尝试使用实用方法org.thymeleaf.expression.Lists

<td th:text="${#lists.size(u.engines)}">[Engine Size]</td>