如何使用 OGNL 调用操作方法

问题描述 投票:0回答:2

如何使用 OGNL 调用操作方法?

helloAction.java

public String getQuote()
{
    return "Don't think, just do";
}

success.jsp

<b>quote() :</b> <s:property value="quote()"/> <br>

struts.xml

<action name="greet" class="com.struts2.helloAction" >
    <interceptor-ref name="firewallStack"></interceptor-ref>
    <result name="SUCCESS">/WEB-INF/resources/success.jsp</result>
    <result name="input">/WEB-INF/resources/success.jsp</result>
</action>

我从 struts 2 OGNL

获得了参考链接

这个

quote()
方法没有被调用。我正在使用 xwork-2.0.1.jar 和 ognl-2.6.11.jar。

java jsp struts2 ognl struts2-interceptors
2个回答
4
投票

你原来的语法几乎是正确的——只需去掉括号即可。

<s:property value="%{quote}" />

JavaBean 争用比显式方法调用更通用,例如,使用 JSP EL:

${quote}

当函数不带参数时,首选 JavaBean 约定。


0
投票

这个 quote() 方法没有被调用。我正在使用 xwork-2.0.1.jar 和 ognl-2.6.11.jar。

你的行动中没有这种方法。如果你创建它:

public String quote() {

并使用正常的 OGNL 方法调用语法:

<s:property value="%{quote()}" />

然后就会根据需要调用。

有关详细信息和语法,您可以阅读OGNL语言指南

© www.soinside.com 2019 - 2024. All rights reserved.