Search code examples
jspstruts2ognl

How to get values from hashmap in struts2 and compare it in <s:if test>?


I have a hashmap in Action class named param(String,String) in which one of the (key,value) pairs are

USE=>1  or USE=>0 

I want to check it in jsp, using whether the "USE" has value "1" or "0"? I have a proper getter in the action class for hashmap.And I also verified it in HTML textfield Whether the value for the key "USE" is coming or not correctly.

I tried

<s:iterator  var="map" value="param" >
  <s:if test="%{#map['USE'].equals(\"1\")}">
    <s:checkbox  label="Use" name="use" fieldValue="true" value="true"labelposition="left"/>
  </s:if>
</s:iterator>
<s:else>
  <s:checkbox  label="Use" name="useMta" fieldValue="false" value="false" labelposition="left"/>
</s:else>

But it is not working? Any idea will be helpful to me.


Solution

  • You don't need to iterate through map to get a value from it. As you doing it now the type of your #map variable is not a Map but Map$Entry.

    So remove <s:iterator> tag and use just <s:if> - <s:else> tags.

    <s:if test="param['USE'].equals(\"1\")">
      <s:checkbox label="Use" name="use" fieldValue="true" value="true"
                  labelposition="left"/>
    </s:if>
    <s:else>
      <s:checkbox label="Use" name="useMta" fieldValue="false" value="false"
                  labelposition="left"/>
    </s:else>
    

    BTW naming your action field as param is probably not the best idea, consider changing it to something less keyword-ish.