1

I am working on jsp, and facing some syntax related issue (that's what I think it is). So, my jsp code has a map that is pushed to jsp through ModelAndView as a Map. In jsp, I want to compare the map.key to a string, if it is equal, then display, otherwise skip that map.key and continue with others.

Code snippet:

<c:forEach items = "${maker_model.kycPoiRejectionCodeMap}" var = "reasonEntry" >
   <c:if test= "${reasonEntry.key eq 'stringToCompare'}">
      //Show reasonEntry.value this if reasonEntry.key is 'stringToCompare'
   </c:if>
   <c:otherwise>
     //else this
    </c:otherwise>
 </c:forEach>

<c:if test= "${reasonEntry.key eq 'stringToCompare'}"> is not working. I have tried <c:if test= "${reasonEntry.key == 'stringToCompare'}"> and this too did not work.

I have already gone through the question: compare-map-keys-with-some-element-and-based-on-the-result-show-its-values and hash-map-key-check-in-jstl but in the solution they have removed the comparison itself and in the 2nd, I am doing same as suggested. I have also searched for other questions but did not find any leads.

Maybe useful: Maybe useful: Values in reasonEntry.key are extracted from enum::name (name() method of Enum.java).

Can someone let me know how to fix this?

Tehreem
  • 476
  • 9
  • 23

3 Answers3

0

You can set the value of stringToCompare into a variable with <c:set...> and compare the map.key to new variable you just set. Or I think the problem is that the map.Key don't return anything I've faced this kind of problem and maybe you shoulde call the getter to get you value like you mentionned map.Name(). It may works I guess.

Mehdi Ziat
  • 71
  • 6
0

I'm guessing here, if this doesn't work let me know and I will delete my answer. But in this line of code here:

<c:forEach items = "${maker_model.kycPoiRejectionCodeMap}" var = "reasonEntry">

I believe the forEach will return values() of your Map as opposed to the entrySet() which is what I think you want. So you can test this by changing this line to:

<c:forEach items = "${maker_model.kycPoiRejectionCodeMap.entrySet}" var = "reasonEntry">

EDIT: So apparently the forEach should return entrySet(), but it would still be interesting to see if explicitly calling entrySet() fixes the problem or not.

mohammedkhan
  • 953
  • 6
  • 14
  • 1
    `I believe the forEach will return values() of your Map as opposed to the entrySet()` - This is wrong. If `maker_model.kycPoiRejectionCodeMap` is a `Map`, each iteration will put the `Entry` into `reasonEntry`. – Arvind Kumar Avinash Jun 14 '20 at 21:23
  • @ArvindKumarAvinash Thanks, I'll add an edit to the comment. Would be interesting to still see if explicitly using `entrySet()` fixes the problem. – mohammedkhan Jun 15 '20 at 13:24
0

I posted the question, I found the issue, it is "I am using IF with OTHERWISE". This was the syntax issue I was facing. The correct syntax is

<c:choose> 
   <c:when>  
   </c:when> 
   <c:otherwise> 
   </c:otherwise> 
</c:choose>
Tehreem
  • 476
  • 9
  • 23