JSTL Check if a value in an array matches second array Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of JSTL Check if a value in an array matches second array without wasting too much if your time.

The question is published on by Tutorial Guruji team.

its me again. Basically Im checking the roles of a user once they hit a page. Its an array, lets say 1,2,3. The contents of the last column on the jsp for testing has the role #’s that are attached to each individual attachment. The last column wont be on the finished product, but I need to do some sort of IF on that array to see if any of the values in the array:

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

are in the array of the attachment roles:

<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>

Now the arrays ont need to match exactly, just a value in the array of user roles is in the array of attachment roles….

I need to do the check here to determin whether or not to put a disable flag on the “delete” button:

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

Answer

Doing 2 nested for loops and having a var set to false initially, then set to true and checking on that var seemed to work

   <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>
We are here to answer your question about JSTL Check if a value in an array matches second array - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji