java - JSTL 检查数组中的值是否与第二个数组匹配

标签 java jsp jstl

又是我。基本上,我会在用户点击页面后检查他们的角色。它是一个数组,比方说 1,2,3。用于测试的 jsp 上最后一列的内容具有附加到每个单独附件的角色 #。最后一列不会出现在成品上,但我需要对该数组执行某种 IF 来查看数组中是否有任何值:

<c:forEach items = "${hotPartRoles}" var = "hpRole"> 
    ${hpRole.id}
    </c:forEach>

在附件角色数组中:

<c:forEach items = "${item.roles}" var = "role"> 
                    ${role.id}
                    </c:forEach>

jsp:

<table class="data_table">
<tr>
    <th>Attachments</th>
    //These are the user's Roles
    <c:forEach items = "${hotPartRoles}" var = "hpRole"> 
    ${hpRole.id}
    </c:forEach>

</tr>
<tr>
    <td class="subtable">
    <table class="data_table">
    <c:choose>
        <c:when test='${empty attachList}'>
           <tr>
              <td>No Attachments</td>
           </tr>
        </c:when>
        <c:otherwise>
            <tr>
                <th>Remove Attachment</th>
                <th>File Name</th>
                <th>File Type</th>
                <th>File Size (bytes)</th>
                <th>File Attached By</th>
                <th>Date/Time File Attached</th>
                <th>Roles</th>
            </tr>
            <c:forEach var="item" items="${attachList}" varStatus="loopCount">
                <tr>

                    <td class="button">
                    <rbac:check operation="<%=Operation.DELETE%>">
                        <button type="button"  onclick="javascript:delete_prompt(${item.id});">Delete</button>
                    </rbac:check>
                        </td>
                    <td><a href="show.view_hotpart_attachment?id=${item.id}">${item.fileName}</a></td>
                    <td>${item.fileType}</td>
                    <td><fmt:formatNumber value="${item.fileSize}" /></td>
                    <td>${item.auditable.createdBy.lastName}, ${item.auditable.createdBy.firstName}</td>
                    <td><fmt:formatDate value="${item.auditable.createdDate}" pattern="${date_time_pattern}" /></td>
                    <td>
                    <c:forEach items = "${item.roles}" var = "role"> 
                    ${role.id}
                    </c:forEach>
                    </td>
                </tr>
            </c:forEach>
        </c:otherwise>
    </c:choose>
    </table>

现在数组不需要完全匹配,只是用户角色数组中的值位于附件角色数组中......

我需要在这里进行检查以确定是否在“删除”按钮上放置禁用标志:

 <rbac:check operation="<%=Operation.DELETE%>">
                        <button type="button"  onclick="javascript:delete_prompt(${item.id});">Delete</button>
                    </rbac:check>
                        </td>
                    <td>

最佳答案

执行 2 个嵌套 for 循环并将 var 最初设置为 false,然后设置为 true 并检查该 var 似乎有效

   <c:forEach var="item" items="${attachList}" varStatus="loopCount">
            <c:set var="dispVal" value="false"/>
            <c:forEach items = "${item.roles}" var = "role"> 
                <c:forEach items = "${hotPartRoles}" var = "hpRole"> 
                        <c:if test="${hpRole.id == role.id}">
                      <c:set var="dispVal" value="true"/>
                        </c:if>
                    </c:forEach>
                </c:forEach>

                <tr>

                    <td class="button">
                    <rbac:check operation="<%=Operation.DELETE%>">

                        <button type="button"<c:if test="${dispVal != 'true'}"> disabled="disabled"</c:if>  
                            onclick="javascript:delete_prompt(${item.id});">Delete</button>

关于java - JSTL 检查数组中的值是否与第二个数组匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9737986/

相关文章:

java - Hibernate映射问题

java - SpringBoot 2.1.x中使用RestTemplateBuilder的requestFactory自定义RestTemplate不向下兼容1.5.x版本

java - 评估多个 fn :contains() conditions with JSTL

java - 如何从 HTML 表格中选定的行获取单元格值?

java - Java 与 JSP 的 JSTL 的性能比较

java - 在 C++ 中创建一个 byte[][] 并使用 JNI 将其返回给 Java

java - 如何使用@ActivityInfo.ScreenOrientation

Java Spring 框架错误

java - 访问 Spring/Maven 应用程序中的资源文件夹

java - 异常 : "No adapter for handler. Does your handler implement a supported interface like controller?"