aspose file tools*
The moose likes Struts and the fly likes chaining actions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "chaining actions?" Watch "chaining actions?" New topic
Author

chaining actions?

Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Hi!

In my webapplications there are footers and menus that are the same on all pages. I have added Tiles to maintain this but do I have to get the footer data and menudata in every action? In the footer there is brand and in the menu categories.

Should I use chain to let the action set brand and subcategories first? What is the best approach when you have the same data on multiple jsp pages.

I am using struts2


SCJP1.4
Revathi Damera
Greenhorn

Joined: May 03, 2007
Posts: 5
U need not add code in action or chain action class, if the footer and menu jsps are correct and don't need any dynamic data, while loading the main jsp page, tiles will take care and load the other footer and menu jsps.

Whatever code needs to maintained in footer and menu, write them in corresponding jsp and specify that in tiles-defs.xml
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
The brands etc are dynamic data. It depends on store and what administrators add to the db
Dom Lassy
Ranch Hand

Joined: May 05, 2006
Posts: 181
For anything that is conditionally displayed in a header or footer, I usually put that information in session scope. So for example, if I have a toolbar.jsp that shows a link for "admin options" only if the user is logged in as an admin, when the admin logs on, I set a session variable or I put an object with preferences in the session, and the toolbar.jsp tile looks like this:

This is quick and dirty since I'm rushed for time, but it should give you an idea on a potential option.

Or, if you can't use session objects, you should be able to use request.setAttribute("whatever", whateverObj) in every single action and in your tiles check for the values in that whateverObject. It might be best to extend Action and put those calls in a preprocess method so it is only in one place.

[ May 18, 2007: Message edited by: Dom Lassy ]
[ May 18, 2007: Message edited by: Dom Lassy ]
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Thanks!

I want to use filters or Interceptors to set session data and open , close hibernate sessions before reaching the Action.

How can I get this chain to work

--- populate object that all actions need
--- execute the action
--- do some clean up that all actions need

I look into the Interceptor interface and abstract classes but it seems that they first execute and then in reverse order. I do not need to populate in reverse order? Any suggestions?
Dom Lassy
Ranch Hand

Joined: May 05, 2006
Posts: 181
How can I get this chain to work

--- populate object that all actions need
--- execute the action
--- do some clean up that all actions need


Create a class that extends Action, called ActionExt. In ActionExt create a preProcess() method and "populate object that all actions need". In ActionExt declare this method:

Also, in ActionExt create a postProcess() method to "do some clean up that all actions need".
Finally, in ActionExt do this:


Then have all of your actions extend ActionExt and implement the abstract executeAction method.

EDIT: There are other ways to handle exceptions so you don't have to implement the exception handling the way I posted.

[ May 18, 2007: Message edited by: Dom Lassy ]
[ May 18, 2007: Message edited by: Dom Lassy ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I use an approach like Dom suggests. I populate the data needed for headers in a base action class. A filter approach like what Struts 2.0/WebWork uses seems cleaner and easier to configure on a per action bases. The template pattern and inheritance can be very powerful, but it does have limitations.

- Brent
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: chaining actions?