1

How to call an action method using 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>

I got the ref link from struts 2 OGNL

This quote() method is not called. I am using xwork-2.0.1.jar and ognl-2.6.11.jar.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Manish Basdeo
  • 6,139
  • 22
  • 68
  • 102

2 Answers2

4

Your original syntax is almost correct–just leave off the parens.

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

JavaBean contentions are more general-purpose than explicit method invocation, e.g., use JSP EL:

${quote}

JavaBean conventions are be preferred when the function takes no arguments.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 1
    It wasn't me, and I in general think it should be the preferred practice in JSPs since that machinery will always be available. But the "preferred" Nazis probably want a link. You have write access to the struts documentation? You should just add a document to that effect and put the link here, that would be funny. – Quaternion Oct 08 '13 at 19:43
  • @Quaternion I once had someone chide me for providing an opinion that wasn't in a book, so I put it in my book so I could quote myself. I gotta finish that other book(s) so I can quote me more ;) – Dave Newton Oct 08 '13 at 19:48
  • @RomanC No, the question is how to call a *specific* action method, which doesn't need to use method-call syntax, because it's a getter. – Dave Newton Jun 11 '14 at 10:59
  • @DaveNewton You should better read the question, _How to call an action method_, then _This quote() method is not called_. Do you understand which method it should call? If it uses a property name to invoke a getter on some bean in the stack it's not calling a method in OGNL. OGNL has a syntax for calling methods on objects `method()`. – Roman C Jun 11 '14 at 18:36
0

This quote() method is not called. I am using xwork-2.0.1.jar and ognl-2.6.11.jar.

You don't have that method in your action. If you create it:

public String quote() {

and use normal OGNL method call syntax:

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

then it will be called as desired.

For details information and syntax you can read OGNL Language Guide.

Roman C
  • 49,761
  • 33
  • 66
  • 176