views:

319

answers:

2

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.

A: 

What about trying the opposite way?

<s:if test="#currentLongValue == Long.parseLong(#stringValue)" >

A side note: I've never used structs2 directly but I worked with grails.. shouldn't you embed the test inside braces?

<s:if test="%{#currentLongValue == Long.parseLong(#stringValue)}" >
Jack
+1  A: 

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>
Omnipresent
Thank you for all your responses. Actually I was hitting the wrong Action handler method, therefore not instantiating bean being used in JSP... silly me.I changed my == to toString().equals() and it works great. Thanks!
Marquinio