struts2ognl

How to test bean property in Struts 2?


I have a class like this:

public class Foo {
    public boolean isValid() {
        return false;
    }
}

In my JSP file I want to use the isValid method in a test condition:

<s:bean name="com.Foo" var="bar"></s:bean>
<s:if test="%{bar.valid == false}">
    <p>hello</p>                    
</s:if>

but it doesn't work. What did I do wrong?


Solution

  • Context variables are referenced by #, but you've used a name bar without number sign.

    <s:if test="%{#bar.valid == false}">
        <p>hello</p>                    
    </s:if>
    

    Look at the Variable References of the OGNL language guide.

    OGNL has a simple variable scheme, which lets you store intermediate results and use them again, or just name things to make an expression easier to understand. All variables in OGNL are global to the entire expression. You refer to a variable using a number sign in front of its name, like this:

    #var