Praveen
Praveen

Reputation: 13

<s:if> tag is not working

I am having trouble with the Struts 2 <s:if>/<s:else> tags–the condition is not matching. It is always going to else block.

I have a variable subMenu in my action with setters and getters. I set the variable to "1" in my action.

I have tried each of the syntaxes below:

<s:if test="%{#subMenu == \"1\"}">
  This is Submenu 1
 </s:if>

 <s:if test="subMenu == '1'">
   You have selected 1. 
 </s:if>

 <s:if test="%{#subMenu == '1'}">
   This is Submenu 1
 </s:if>

 <s:if test="%{subMenu == '1'}">
   This is Submenu 1
 </s:if>

Upvotes: 1

Views: 2593

Answers (3)

Dave Newton
Dave Newton

Reputation: 160191

If subMenu is a String as the tests imply, use double-quotes, otherwise the immediate value will be interpreted as a char (not String) because it's a single character in single quotes.

<s:if test='%{subMenu == "1"}'>
  This is Submenu 1
</s:if>

Note that I've flipped which quotes are used where. This is just how OGNL works.

Alternatives include using an actual integer, or having an easier-to-reason-about system beyond "1", "2", "3", etc. and using contextually-meaningful names instead of making people think about it.

Upvotes: 2

Priya
Priya

Reputation: 1

For Single characters struts if comparison didnt work.Easy solution is try using a string instead of a single char

Upvotes: 0

Jaiwo99
Jaiwo99

Reputation: 10017

here could be the solution. if the result of your action is "plain" and subMenu is type "string", which means, it is a page

<s:if test="subMenu == '1'">test</s:if>

should work.

here is the suggestion.

  1. try print "subMenu" first to check, if the variable not null by using:

    <s:property value="subMenu"/>
    
  2. check your action class, if your result is not *.jsp, but *.action, you will lose your variable, try add your variable as parameter like this:

    <result name="success">
        <param name="subMenu">${subMenu}</param>
        <param name="location">*.action</param>
        ...
    </result>
    

Upvotes: 1

Related Questions