Friday, February 20, 2015

Struts: Tricky s:if tag syntax

tag is very useful for me especially when I want to manipulate display of data in JSP. I use it often. But then, one day I encountered an odd bug that i can't seem to find solution in a few days. Syntax of tag is
<s:if test="%{memberCd == 'OVERSEAS WORKER'}"> 
Code:

 <s:if test="%{memberCd == 'OVERSEAS WORKER'}">     
  <tr>  
       <td colspan="1" id="labels"><label>Country:</label></td>  
       <td colspan="3" class="labelVal"><s:label cssClass="labelVal" id="ofwCountry" value="%{ofwCountry}"/></td>           
  </tr>       
  <tr>  
       <td colspan="1" id="labels"><label>Monthly Earnings:</label></td>  
       <td colspan="3" class="labelVal"><label class="label">(Php)</label>&nbsp;<s:label cssClass="labelVal" id="salary" value="%{salary}"/> </td>  
  </tr>     
  <s:if test="%{memberSubCd=='A'}">  
  <tr>  
       <td colspan="1" id="labels"><label>Flexi Fund Member:</label></td>  
       <td colspan="3" class="labelVal"><label>YES</label></td>  
  </tr>   
  </s:if>  
 </s:if>  

But inner <s:if> (highlighted)is not showing even though the statement is true. 

Solution:
1. Change the order of single quote and double quote. Take note that in the original syntax, the double quote is used to quote the outer part, while the single quote is used for the inner part. To solve the problem, use single quote for the outer and the double quote for the inner. 
From this  
 <s:if test="%{memberSubCd=='A'}">   
to this:
 <s:if test='%{memberSubCd=="A"}'>   

2. Use equalsIgnoreCase comparison statement of Java
From this  
 <s:if test="%{memberSubCd=='A'}">   
to this:
 <s:if test='%{memberSubCd.equalsIngnoreCase("A")}'>   

Hope this helps. If there is another way, then feel free to comment. Thanks!

No comments:

Post a Comment