How to test bean property in Struts 2?

雨燕双飞 提交于 2020-01-13 19:36:50

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/40046842/how-to-test-bean-property-in-struts-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!