You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by gd...@jslss.com on 2009/06/25 20:08:14 UTC

Struts 2.1 Accessing the action instance from a JSP

Does anyone know how to access the action instance from the value stack. 
I'd like to be able to call a method on the action from the JSP.

I'm trying to do something like this:

MyAction.java:
public class MyAction extends ActionSupport
{
    public String myMethod()
    {
        return "something";
    }
}

JSP:
<%

   MyAction myAction = [##somehow get the action instance##];
   String myMethodResult = myAction.myMethod();

%>

Thanks in advance,
Graham



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 2.1 Accessing the action instance from a JSP

Posted by gd...@jslss.com.
I was able to get access to the action from the JSP as follows (it isn't
pretty but it worked)

JSP:

<%
com.opensymphony.xwork2.ognl.OgnlValueStack stack =
(com.opensymphony.xwork2.ognl.OgnlValueStack)request.getAttribute("struts.valueStack");

MyAction myAction = (MyAction)stack.findValue("action");

String myMethodResult = myAction.myMethod();

%>

Regards,
Graham


On Thu, June 25, 2009 2:21 pm, Wes Wannemacher wrote:
> Does it have to be in a scriptlet? OGNL will let you call methods,
> assuming the action is on top of the stack (you aren't inside of another
> action from the s:action tag, or you aren't in an iterator that is
> iterating an object that has a matching method) -
>
> <s:property value="%{myMethod()}" />
>
>
> -Wes
>
>
> On Thu, Jun 25, 2009 at 2:08 PM, <gd...@jslss.com> wrote:
>
>>
>> Does anyone know how to access the action instance from the value
>> stack. I'd like to be able to call a method on the action from the JSP.
>>
>>
>> I'm trying to do something like this:
>>
>>
>> MyAction.java:
>> public class MyAction extends ActionSupport {
>>    public String myMethod()
>>    {
>>        return "something";
>>    }
>> }
>>
>>
>> JSP:
>> <%
>>
>>
>>   MyAction myAction = [##somehow get the action instance##];
>>   String myMethodResult = myAction.myMethod();
>>
>>
>> %>
>>
>>
>> Thanks in advance,
>> Graham
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>>
>
>
>
> --
> Wes Wannemacher
> Author - Struts 2 In Practice
> Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more
> http://www.manning.com/wannemacher
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 2.1 Accessing the action instance from a JSP

Posted by Wes Wannemacher <we...@wantii.com>.
Does it have to be in a scriptlet? OGNL will let you call methods,
assuming the action is on top of the stack (you aren't inside of
another action from the s:action tag, or you aren't in an iterator
that is iterating an object that has a matching method) -

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

-Wes

On Thu, Jun 25, 2009 at 2:08 PM, <gd...@jslss.com> wrote:
>
> Does anyone know how to access the action instance from the value stack.
> I'd like to be able to call a method on the action from the JSP.
>
> I'm trying to do something like this:
>
> MyAction.java:
> public class MyAction extends ActionSupport
> {
>    public String myMethod()
>    {
>        return "something";
>    }
> }
>
> JSP:
> <%
>
>   MyAction myAction = [##somehow get the action instance##];
>   String myMethodResult = myAction.myMethod();
>
> %>
>
> Thanks in advance,
> Graham
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>



-- 
Wes Wannemacher
Author - Struts 2 In Practice
Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more
http://www.manning.com/wannemacher

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org