Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two strings using Struts2 tags and OGNL?

I am trying to compare two values : one from session and anther from iterator

<s:iterator value="themes" status="currentRecord"> 
    <s:if test="%{usertheme}) == %{themeName}">
        <td align="center" bgcolor="red">
    </s:if>
    <s:else>
        <td align="center" bgcolor="green">
    </s:else>
</s:iterator>

But I am unable to compare my values, please can you tell me where I am doing mistakes ?

like image 378
Dan Avatar asked Dec 24 '12 15:12

Dan


1 Answers

%{} should be put (if necessary) around all the statement, not in the middle.

For Strings you should use .equals, .equalsIgnoreCase, .contains, .indexOf etc... Not ==.

Change to this:

<s:iterator value="themes" status="currentRecord"> 
   <s:if test="%{#session.usertheme.equalsIgnoreCase(themeName)}">
      <td align="center" bgcolor="red">
   </s:if>
   <s:else>
      <td align="center" bgcolor="yellow">
   </s:else>
....

this works too:

   <s:if test="#session.usertheme.equalsIgnoreCase(themeName)">
like image 98
Andrea Ligios Avatar answered Sep 22 '22 12:09

Andrea Ligios