Search code examples
javajspstruts2ognl

JSP comparing hashmap key with an object value


I am new to JSP and I am facing issue in executing a comparison of hashmap key and an object value. I have an object (Box) which has a string field (size). I have a hashmap also which contains list of Boxes based on size

Map<String, List<Box> boxCatgMap=new HashMap<String, List<Box>>();

  <s:iterator value="boxCatgMap" var="boxCatg">
        <s:set var="boxCatgKey" value="#boxCatg.key"/>
        <s:iterator value="boxes" var="box" status="ind">
           <s:if test="%{#box.size.equals(boxCatgKey)}">
            //some code
           </s:if></s:iterator>
  </s:iterator>

The if condition has some issue because of which it is not getting executed. Can any one please help what am I missing?

Thanks


Solution

  • Use hash (#) in front of boxCatgKey too.

    <s:if test="%{#box.size.equals(#boxCatgKey)}"></s:if>
    

    When you use Struts 2 data-tags with the default scope (i.e action scope) :

    <s:set var="boxCatgKey" value="#boxCatg.key"/>
    

    You can access it by #boxCatgKey.