JSP-JSTL如果需要检查用户列表是否包含特定用户,如何写if语句?

How do I write an if statement in JSP-JSTL if I need to check whether the user list contains a specific user?

'theGroup' 和 'groupCreator' 是来自控制器的模型(实体)属性。 'users' 是数组列表,它是 theGroup 的属性(theGroup.getUsers() 基本上)。

我需要检查 groupCreator 是否存在于 JSP 的 theGroup.users 列表中

我试过下面的代码,但没有用

<c:if test = "${theGroup.users.contains(groupCreator)}">

</c:if>

您可以使用 forEach 遍历值,然后将其中的值与 groupCreator 进行比较。您的代码将如下所示:

     <c:forEach var="values" items="${theGroup.users}">
        <c:if test="${values == groupCreator}">
            <!--setting true if value match-->
            <c:set var="Matched" value="true" scope="request" />
        </c:if>
     </c:forEach
     <!--Print-->
        ${Matched}