桌子上的复选标记(JSTL)

时间:2015-06-22 09:34:08

标签: java html jsp servlets jstl

我有以下课程:

public class User {
    private String username;
    private String password;
    private List<Integer> roles;
    // Getters and Setters
}

我有一个JSP,可以查看当前注册的所有用户。所以我希望它是他们的用户名和他们拥有的角色的摘要。角色是包含他们可以查看的页面索引的列表。

示例:

+------------+--------+--------+--------+
| Username   | Page 1 | Page 2 | Page 3 |
+------------+--------+--------+--------+
| user1      |   ✔                 ✔   |
+------------+--------+--------+--------+
| user2      |            ✔        ✔   |
+------------+--------+--------+--------+
| user3      |   ✔                 ✔   |
+------------+--------+--------+--------+

现在这就是我所拥有的:

<table cellpadding= "5">
    <tr>
        <th>Username</th>
        <th>Page 1</th>
        <th>Page 2</th>
        <th>Page 3</th>
    </tr>
  <c:forEach items="${users}" var="user">
    <tr align="left">
        <td>
            <c:out value="${user.username}"/>
        </td>
        /*insert conditional here*/
    </tr>
  </c:forEach>
</table>

1 个答案:

答案 0 :(得分:0)

there is no built-in support in JSTL for list.contains()起,您必须回退到每次循环整个列表的黑客行为,检查是否存在当前所需的值:

  <c:forEach items="${users}" var="user">
    <tr align="left">
        <td>
            <c:out value="${user.username}"/>
        </td>

        <td>
            <c:forEach var="role" items="${user.roles}">
                <c:if test="${role eq 1}"> ✔ </c:if>
            </c:forEach>
        </td>
        <td>
            <c:forEach var="role" items="${user.roles}">
                <c:if test="${role eq 2}"> ✔ </c:if>
            </c:forEach>
        </td>
        <td>
            <c:forEach var="role" items="${user.roles}">
                <c:if test="${role eq 3}"> ✔ </c:if>
            </c:forEach>
        </td>
    </tr>
  </c:forEach>

这对JSTL来说非常难过;我已经习惯了OGNL(由Struts2使用)和it would be simply

<s:if test="1 in user.roles"> ✔ </s:if>

那就是说,请注意you should never carry a password like that(并且根本不带