You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by fahlen <ma...@spray.se> on 2006/11/30 21:32:18 UTC

Struts 2 and passing data using interceptors

Hi. I'm new to Struts and have a question regarding how data is passed from
an Action (in Struts 2) to the JSP which one is redirected to after the
action is executed. Reading the documentation on Struts 2, I've found that
using the Scope Interceptor one can specify Action properties to be bound to
the session or application scope. This is configured in struts.xml with the
<interceptor-ref name="scope"> tag and then setting the appropriate
parameters. See http://www.twdata.org/backups/WW/scope-interceptor.html

Now, say I access an EJB from an Action. The method of the EJB returns some
data. Is it good programming practice (in Struts) to store this data in the
session object, later to be accessed from a JSP? This should allow me to
pass the data, but perhaps there is some other alternative which is
preferred. Storing information in the session object would maybe be good
practice for data such as user id, contents of a shopping cart, etc. The
scope I believe I am interested in is request scope, since the data returned
from the invoked EJB method is data from an Amazon.com search, only to be
used in the JSP displayed immediately after the execution of the Action.

Would the preferred method in my case then be to implement the
ServletRequestAware interface in the Action and store data of interest in
the request object? I can't find any interceptor that does this. I'm sure
one can create user defined interceptors, but if there's a standard method
for this functionality provided by the framework, then I guess that would be
the preferred way to go.

I'm grateful for any input on the matter. Cheers.
-- 
View this message in context: http://www.nabble.com/Struts-2-and-passing-data-using-interceptors-tf2734105.html#a7627298
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: Struts 2 and passing data using interceptors

Posted by fahlen <ma...@spray.se>.
Hi Mark!

I replaced the line

    <result type="redirect-action">Display</result>

with

    <result name="success">/crosseyed/Display.jsp</result>

as you suggested, and now it works :) Thank you for all your help!!


Mark Menard wrote:
> 
> On 12/2/06 10:45 AM, "fahlen" <ma...@spray.se> wrote:
> 
>> 
>> Hi Mark,
>> 
>> I've tried this now, but I'm unable to access the data from the JSP. In
>> the
>> struts.xml configuration file, the action involved is configured this
>> way:
>> 
>>       <action name="Search_*" method="{1}" class="crosseyed.Search">
>>           <result name="input">/crosseyed/Search.jsp</result>
>>           <result type="redirect-action">Display</result>
>>       </action>
>> 
>> I.e. Search.jsp -> Search action class -> Display.jsp
>> 
>> On the Search.jsp page, one can enter some keywords. The Search action is
>> then executed, after which one is redirected to the Display.jsp which
>> would
>> display the results (data received from the session bean). I've tried
>> setting the value of the Search action property I'm trying to access from
>> Display.java to a constant value, but even then nothing is output in the
>> JSP. I'm the syntax you specified in your reply:
>> 
>> <s:property value="%{dataFromSessionBean}" />
>> 
>> The dataFromSessionBean has it's getter and setter methods defined.
> 
> So, just so I'm clear, on your crosseyeed.Search class you have a
> setDataFromSessionBean() a getDataFromSessionBean() method?
> 
> Now you're also just trying to get from Search.jsp to Display.jsp with
> Search action in the middle right? If yes, try changing the:
> 
>     <result type="redirect-action">Display</result>
> 
> to:
>    
>     <result name="success">/crosseyed/Result.jsp</result>
> 
> And just return "seccess" from your execute() method after you have
> populated dataFromSessionBean.
> 
>> My spontaneous question is if one has to define somewhere (such as in the
>> struts.xml file) that the action object be placed on some stack. Or do I
>> have to define the name of the Action also, when using the s:property
>> tag?
>> I've tried various other ways, like <s:property
>> value="%{Search.dataFromSessionBean}" /> among others, but none works.
> 
> You don't have to define anything in the struts.xml file. The action
> instance is always pushed onto the stack, and is usually the top bean on
> the
> stack, unless you are using the ModelDriven method.
> 
> Mark
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Struts-2-and-passing-data-using-interceptors-tf2734105.html#a7666816
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: Struts 2 and passing data using interceptors

Posted by Mark Menard <ma...@mjm.net>.
On 12/2/06 10:45 AM, "fahlen" <ma...@spray.se> wrote:

> 
> Hi Mark,
> 
> I've tried this now, but I'm unable to access the data from the JSP. In the
> struts.xml configuration file, the action involved is configured this way:
> 
>       <action name="Search_*" method="{1}" class="crosseyed.Search">
>           <result name="input">/crosseyed/Search.jsp</result>
>           <result type="redirect-action">Display</result>
>       </action>
> 
> I.e. Search.jsp -> Search action class -> Display.jsp
> 
> On the Search.jsp page, one can enter some keywords. The Search action is
> then executed, after which one is redirected to the Display.jsp which would
> display the results (data received from the session bean). I've tried
> setting the value of the Search action property I'm trying to access from
> Display.java to a constant value, but even then nothing is output in the
> JSP. I'm the syntax you specified in your reply:
> 
> <s:property value="%{dataFromSessionBean}" />
> 
> The dataFromSessionBean has it's getter and setter methods defined.

So, just so I'm clear, on your crosseyeed.Search class you have a
setDataFromSessionBean() a getDataFromSessionBean() method?

Now you're also just trying to get from Search.jsp to Display.jsp with
Search action in the middle right? If yes, try changing the:

    <result type="redirect-action">Display</result>

to:
   
    <result name="success">/crosseyed/Result.jsp</result>

And just return "seccess" from your execute() method after you have
populated dataFromSessionBean.

> My spontaneous question is if one has to define somewhere (such as in the
> struts.xml file) that the action object be placed on some stack. Or do I
> have to define the name of the Action also, when using the s:property tag?
> I've tried various other ways, like <s:property
> value="%{Search.dataFromSessionBean}" /> among others, but none works.

You don't have to define anything in the struts.xml file. The action
instance is always pushed onto the stack, and is usually the top bean on the
stack, unless you are using the ModelDriven method.

Mark

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


Re: Struts 2 and passing data using interceptors

Posted by fahlen <ma...@spray.se>.
Hi Mark,

I've tried this now, but I'm unable to access the data from the JSP. In the
struts.xml configuration file, the action involved is configured this way:

        <action name="Search_*" method="{1}" class="crosseyed.Search">
            <result name="input">/crosseyed/Search.jsp</result>
            <result type="redirect-action">Display</result>
        </action>

I.e. Search.jsp -> Search action class -> Display.jsp

On the Search.jsp page, one can enter some keywords. The Search action is
then executed, after which one is redirected to the Display.jsp which would
display the results (data received from the session bean). I've tried
setting the value of the Search action property I'm trying to access from
Display.java to a constant value, but even then nothing is output in the
JSP. I'm the syntax you specified in your reply:

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

The dataFromSessionBean has it's getter and setter methods defined.

My spontaneous question is if one has to define somewhere (such as in the
struts.xml file) that the action object be placed on some stack. Or do I
have to define the name of the Action also, when using the s:property tag?
I've tried various other ways, like <s:property
value="%{Search.dataFromSessionBean}" /> among others, but none works.

Thanks again.


Mark Menard wrote:
> 
> On 11/30/06 5:29 PM, "Don Brown" <do...@gmail.com> wrote:
> 
>> If you only need request-scoped data, you don't need to bother with
>> the ScopeInterceptor or even the ServletRequestAware interface.
>> Simply define a getter on your action that returns the object in
>> question, then access it via your JSP via JSP EL (JSP 2.0), a JSTL
>> expression, or a Struts 2 property tag.
> 
> To expand on Don's answer. (I know he's busy. Thanks Don.)
> 
> Let's say you have the following Action (this is not a complete listing):
> 
> 
> public class MyAction extends ActionSupport {
> 
>   private String dataRetrievedFromSessionBean;
> 
>   public String execute () throws Exception {
>     MySessionBean sb = getSessionBean ();
>     this.dataRetrievedFromSessionBean = sb.getMyData ();
>     return SUCCESS;
>   }
> 
>   public String getDataRetrievedFromSessionBean () {
>     return this.dataRetrievedFromSessionBean;
>   }
> 
>   private MySessionBean getSessionBean () {
>     // Some code to get your EJB.
>   }
> }
> 
> In your JSP you can display this data with the <s:property> tag:
> 
>     <s:property value="%{dataRetrievedFromSessionBean}" />
> 
> That's it.
> 
> You don't need to mess with Session scope, other interceptors, or any of
> that stuff. It's very simple and elegant. Almost too much so. It eluded me
> for a while, but now I'm really starting to get it, and it's good.
> 
> Mark
> -- 
> Mark Menard
> personal: http://www.vitarara.org/
> business: http://www.vitarara.net/
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Struts-2-and-passing-data-using-interceptors-tf2734105.html#a7654246
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: Struts 2 and passing data using interceptors

Posted by fahlen <ma...@spray.se>.
Thank you all for your replies! They have been very helpful. I'm happy to
have found such an active forum.

Thanks again.
-- 
View this message in context: http://www.nabble.com/Struts-2-and-passing-data-using-interceptors-tf2734105.html#a7634983
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: Struts 2 and passing data using interceptors

Posted by James Mitchell <jm...@apache.org>.
What makes it not thread safe?  If you are thinking in terms of s1, then
yes, but s2 actions are not reused and can therefore keep state as you
normally would have had to put in a s1 formbean.

On 11/30/06, Christopher Schultz <ch...@christopherschultz.net> wrote:
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Mark,
>
> Mark Menard wrote:
> >   public String getDataRetrievedFromSessionBean () {
> >     return this.dataRetrievedFromSessionBean;
> >   }
>
> Sorry for poking my nose in, but wouldn't this be horrendously
> non-threadsafe?
>
> Since I know virtually nothing about S2, I may be completely missing
> something like action instances now live in the session or something
> crazy like that.
>
> Instead of using a method on the action, why not stuff the object into
> the request?
>
> request.setAttribute("dataRetrievedFromSessionBean",
>                       this.getSessionBean());
>
> Sorry if I don't know what I'm talking about.
>
> - -chris
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.5 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFFb3sN9CaO5/Lv0PARAqmjAJ43fvmsWmR0VNEauKXZOjH8f+GORgCcCl/B
> 2HGBwx4xh0OCNwY8QRFmjaU=
> =u3oN
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
James Mitchell
678.910.8017

Re: Struts 2 and passing data using interceptors

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mark,

Mark Menard wrote:
> On 11/30/06 7:45 PM, "Christopher Schultz" <ch...@christopherschultz.net>
> wrote:
>> Sorry for poking my nose in, but wouldn't this be horrendously
>> non-threadsafe?
> 
> No problem. If your thinking of Actions from the perspective of S1, yes,
> this would be horribly non-thread safe. In S2 an action instance is created
> for each request. They are not singletons. So, this is fine.

Yup, I was thinking of S1 actions, so thanks for setting me straight.
Once of these days I've got to read "everything you ever wanted to know
about moving from S1 to S2". Anyhow know if such a thing exists? ;)

Thanks,
- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFcDoz9CaO5/Lv0PARAqZ2AJ0fdk6nH//QlwIiOvk4ghEozrKj4QCgrWWd
eMf3FFWwefGTHrp+4F7GKH0=
=KTr3
-----END PGP SIGNATURE-----

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


Re: Struts 2 and passing data using interceptors

Posted by Mark Menard <ma...@mjm.net>.
On 11/30/06 7:45 PM, "Christopher Schultz" <ch...@christopherschultz.net>
wrote:
> Mark Menard wrote:
>>   public String getDataRetrievedFromSessionBean () {
>>     return this.dataRetrievedFromSessionBean;
>>   }
> 
> Sorry for poking my nose in, but wouldn't this be horrendously
> non-threadsafe?

No problem. If your thinking of Actions from the perspective of S1, yes,
this would be horribly non-thread safe. In S2 an action instance is created
for each request. They are not singletons. So, this is fine.
 
> Since I know virtually nothing about S2, I may be completely missing
> something like action instances now live in the session or something
> crazy like that.

They don't live in the session. In a sense they live in the request scope. I
say that because the Action itself also takes on the role formerly done by
the ActionForm in S1. It's just a JavaBean with setters and getters that is
used to store the information you're interested in, the stuff submitted to
you and what you want to present in your view.

In S2 if you have an accessor for let's say firstName (i.e. getFirstName()
and setFirstName() ) on your Action class, and you have a firstName input in
your HTML form, S2 will call setFirstName() on your action with the
submitted value. This gives you access to the parameters submitted by your
user.

It's really quite simple and elegant. You can also use any JavaBean you'd
like as your form in S2 if you want. (cf:
http://cwiki.apache.org/WW/model-driven-interceptor.html)

Likewise anything that you want to display in your view would also be a
property of your Action. Just get the data in your execute() method and make
it available via a getter and you can display it via your view.

Mark
-- 
Mark Menard
personal: http://www.vitarara.org/
business: http://www.vitarara.net/

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


Re: Struts 2 and passing data using interceptors

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mark,

Mark Menard wrote:
>   public String getDataRetrievedFromSessionBean () {
>     return this.dataRetrievedFromSessionBean;
>   }

Sorry for poking my nose in, but wouldn't this be horrendously
non-threadsafe?

Since I know virtually nothing about S2, I may be completely missing
something like action instances now live in the session or something
crazy like that.

Instead of using a method on the action, why not stuff the object into
the request?

request.setAttribute("dataRetrievedFromSessionBean",
                      this.getSessionBean());

Sorry if I don't know what I'm talking about.

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFb3sN9CaO5/Lv0PARAqmjAJ43fvmsWmR0VNEauKXZOjH8f+GORgCcCl/B
2HGBwx4xh0OCNwY8QRFmjaU=
=u3oN
-----END PGP SIGNATURE-----

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


Re: Struts 2 and passing data using interceptors

Posted by Mark Menard <ma...@mjm.net>.
On 11/30/06 5:29 PM, "Don Brown" <do...@gmail.com> wrote:

> If you only need request-scoped data, you don't need to bother with
> the ScopeInterceptor or even the ServletRequestAware interface.
> Simply define a getter on your action that returns the object in
> question, then access it via your JSP via JSP EL (JSP 2.0), a JSTL
> expression, or a Struts 2 property tag.

To expand on Don's answer. (I know he's busy. Thanks Don.)

Let's say you have the following Action (this is not a complete listing):


public class MyAction extends ActionSupport {

  private String dataRetrievedFromSessionBean;

  public String execute () throws Exception {
    MySessionBean sb = getSessionBean ();
    this.dataRetrievedFromSessionBean = sb.getMyData ();
    return SUCCESS;
  }

  public String getDataRetrievedFromSessionBean () {
    return this.dataRetrievedFromSessionBean;
  }

  private MySessionBean getSessionBean () {
    // Some code to get your EJB.
  }
}

In your JSP you can display this data with the <s:property> tag:

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

That's it.

You don't need to mess with Session scope, other interceptors, or any of
that stuff. It's very simple and elegant. Almost too much so. It eluded me
for a while, but now I'm really starting to get it, and it's good.

Mark
-- 
Mark Menard
personal: http://www.vitarara.org/
business: http://www.vitarara.net/

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


Re: Struts 2 and passing data using interceptors

Posted by Don Brown <do...@gmail.com>.
If you only need request-scoped data, you don't need to bother with
the ScopeInterceptor or even the ServletRequestAware interface.
Simply define a getter on your action that returns the object in
question, then access it via your JSP via JSP EL (JSP 2.0), a JSTL
expression, or a Struts 2 property tag.

Don

On 11/30/06, fahlen <ma...@spray.se> wrote:
>
> Hi. I'm new to Struts and have a question regarding how data is passed from
> an Action (in Struts 2) to the JSP which one is redirected to after the
> action is executed. Reading the documentation on Struts 2, I've found that
> using the Scope Interceptor one can specify Action properties to be bound to
> the session or application scope. This is configured in struts.xml with the
> <interceptor-ref name="scope"> tag and then setting the appropriate
> parameters. See http://www.twdata.org/backups/WW/scope-interceptor.html
>
> Now, say I access an EJB from an Action. The method of the EJB returns some
> data. Is it good programming practice (in Struts) to store this data in the
> session object, later to be accessed from a JSP? This should allow me to
> pass the data, but perhaps there is some other alternative which is
> preferred. Storing information in the session object would maybe be good
> practice for data such as user id, contents of a shopping cart, etc. The
> scope I believe I am interested in is request scope, since the data returned
> from the invoked EJB method is data from an Amazon.com search, only to be
> used in the JSP displayed immediately after the execution of the Action.
>
> Would the preferred method in my case then be to implement the
> ServletRequestAware interface in the Action and store data of interest in
> the request object? I can't find any interceptor that does this. I'm sure
> one can create user defined interceptors, but if there's a standard method
> for this functionality provided by the framework, then I guess that would be
> the preferred way to go.
>
> I'm grateful for any input on the matter. Cheers.
> --
> View this message in context: http://www.nabble.com/Struts-2-and-passing-data-using-interceptors-tf2734105.html#a7627298
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> 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