Search code examples
javajspstruts2ognl

Compare Long values Struts2


Hi everyone I'm trying to compare two values using struts2 s:if tag but its not working. If I hardcode the values it works but I want it to be dynamic.

The variable stringValue is of type String. The variable currentLongValue is of type Long.

<s:set var="stringValue" value="order"/>
<s:iterator value="listTest">
 <s:set var="currentLongValue" value="value"/>

 <s:if test="#currentLongValue.toString() == #stringValue" >
   //Do something      
 </s:if>
 <s:else>
 //Do something else
 </s:else>

</s:iterator>

For the s:if I have tried toString and also the equals(). It only works if I hardcode the values. Example:

<s:if test="#currentLongValue == 1234">

Any clues?

Thank you.


Solution

  • String comparisons should be done using equals() not ==

    Action:

    public Long getSomeLongValue () {
        Long l = 55l;
        return l;
    }
    
    public String getSomeString () {
        return "55";
    }
    

    JSP:

    <s:if test="someLongValue.toString().equals(someString)">
        CAME IN IF
    </s:if>
    <s:else>
        CAME IN SIDE ELSE
    </s:else>