如何在 Struts 2 中测试 bean 属性?

How to test bean property in Struts 2?

我有一个 class 这样的:

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

在我的 JSP 文件中,我想在测试条件下使用 isValid 方法:

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

但它不起作用。我做错了什么?

上下文变量由 # 引用,但您使用的名称 bar 没有数字符号。

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

看看OGNL语言指南的Variable References

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