jspjstl

How to write <c:if> - jstl tag - comparing map key with bean property


I need to retrieve list values from a map of type Map<String,List<HashMap<String, Object>>> in JSP based on a condition. The condition is to compare map key with bean property. Rightnow, I am doing multi level iterations. Firstly, I am iterating the map to retrieve key and an inner iterate loop to retrieve list values.

So far, I have like this

<c:forEach items="${addRatingExceptionForm.ratingsMap}" var="entry"> 
  <c:set var="key" value="${entry.key}"/>
  <jsp:useBean id="key" type="java.lang.String" />
  <c:if test= '<%= key.equalsIgnoreCase(addRatingExceptionForm.getRatingElementDropdown()) %> ' >
    <c:forEach items="${entry.value}" var="item"> 
      <li>
        <input type="checkbox" id="addRatingException_timeline_earlyAsn" value="${item.RatingInstanceValue}"  class="ajaxContentTrigger  method_Load_exceptionType ajaxLoadingTrigger|addRatingException_exceptionType clearErrors"/>  
        <label for="addRatingException_timeline_earlyAsn">${item.RatingInstanceValue}</p></label>   
      </li>
    </c:forEach>
  </c:if>
</c:forEach> 

But it errors on the <c:if> tag.


Solution

  • You don't need to iterate over the map to compare keys. You just have to use the brace notation [] to get a map value by a dynamic key like so ${map[key]}.

    So, this should do:

    <c:forEach items="${addRatingExceptionForm.ratingsMap[addRatingExceptionForm.ratingElementDropdown]}" var="item"> 
      <li>
        <input type="checkbox" id="addRatingException_timeline_earlyAsn" value="${item.RatingInstanceValue}" class="ajaxContentTrigger method_Load_exceptionType ajaxLoadingTrigger|addRatingException_exceptionType clearErrors" />
        <label for="addRatingException_timeline_earlyAsn">${item.RatingInstanceValue}</p></label> <!-- wtf is that </p> doing there? -->
      </li>
    </c:forEach>