Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Struts and the fly likes Struts 2 and passing data using interceptors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2 and passing data using interceptors" Watch "Struts 2 and passing data using interceptors" New topic

Struts 2 and passing data using interceptors

Markus Fahl�n

Joined: Nov 30, 2006
Posts: 2
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

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 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.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Struts 2 question huh....this could get confusing since Struts 1.x and Struts 2 are so different. I played around a little with WebWork. My understanding is that you would store properties that are accessed by your JSP as properties of your action class. WebWork (and Struts 2) has a concept called a Value Stack. It is a little hard to explain and I don't fully understand how it works (magic?) but your Action class is placed on the top of the value stack where its properties can be seen by your JSP page.

If you search around the web for phrases like "WebWork Value Stack" I am sure you can find out more information. Since I don't get the feeling that many people on this forum are using Struts 2 (I am not), you might have to stick around and educate the rest of us.

- Brent
Markus Fahl�n

Joined: Nov 30, 2006
Posts: 2
Thank you for your answer. Hopfully one day I'll be able to lend someone a hand.

I hope there isn't any rule about including references to other forums. In any case, I posted the same question in one other forum and I've gotten a few replies. If you're interested, please have a look:
I agree. Here's the link:
subject: Struts 2 and passing data using interceptors
jQuery in Action, 3rd edition