Search code examples
jspstruts2ognlstruts-tags

struts2 s:if tag behaves differently for string and boolean


I have an s:iterator with s:if like below

<s:iterator var="connections" value="serviceItems">
   <s:if test='disableCheckBox == "Y"'>
      disableCheckBox works
   </s:if>
   <s:if test='#{troubleSupported.equals("true")}'>
     troubleWorks
   </s:if>
</s:iterator>

The above code works, but if I change the second if like below, it does not work.

<s:if test="troubleSupported=='true'">
<s:if test="troubleSupported.equals('true')">

Can someone please explain what could be reason for this difference. Am I doing something wrong here?

The only difference I could see is, the action variable is declared as a boolean for troubleSupported and String for disableCheckBox (Note: Not directly, but through some intermittent Vectors). Could this affect the outcome like what I am seeing.

P.S: To complicate this a bit further, if I swap the single and double quotes for the working scenario,

<s:if test="disableCheckBox == 'Y'"> does not work,

but <s:if test="#{troubleSupported.equals('true')}"> continues to work.

Currently, I'm not looking at this. I am confused enough with my original issue. :)


Solution

  • <s:if test="troubleSupported=='true'"> is not a correct way to check equality in java, and many more languages. Correct way is : <s:if test="troubleSupported===true">. Far better is : <s:if test="troubleSupported"> because you want to test if your boolean is true, so keep it simple.

    You can check boolean values with thymeleaf-like expressions too, but it is more verbose : <s:if test="#{troubleSupported.equals('true')}">.