Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extending Action class

Status
Not open for further replies.

Luca200

Programmer
Jul 7, 2002
13
0
0
IT
I'm building my first application with Struts. I would need to have some data available every time my Actions' execute() method is performed. So I was trying to find the right place to put the code where I load this data (I get them from a MessageResources).

Some solutions I thought about:
1) Having an "ActionXXX extends Action", making its execute() method that does just those things, and then calls another method that would be execute()'s substitute for my single Actions. That would mean when I create each Action ("extends ActionXXX") I would create the new method instead of execute(). I don't like this solution 'cause it would have a great lack of portability in my Actions, having another method instead of execute(). Another matter is that data would be loaded for every request, while I know it can't change (it's binded to the server the application is running on - I must run the same application on many servers with different functionalities).

2) Having, again, an "ActionXXX extends Action", with a field containing the value I need and a snippet of code that evaluates it when the object is instantiated. This would be the code:


public abstract class ActionXXX extends Action {
boolean tower;
{
MessageResources msgs = (MessageResources)servlet.getServletContext().getAttribute("entrate.ambito");
String centrale = msgs.getMessage("ambito.centrale");
tower = "true".equals(centrale) ? false : true;
}
}

With this I would have my "tower" field available in my single Actions that extend this class. The matter, again, is that, for getting servletContext, I obviously need the servlet, but I'm afraid the Action's servlet field is null at the time this code runs.
3) Last solution seems to be to extend the RequestProcessor too, leaving in my ActionXXX just the boolean field with a setter, and making my RequestProcessor call that setter just after having called instance.setServlet(this.servlet) in processActionCreate() method. Sorry for my question not being too short... what about it all?? Thanks in advance!

 

You can get the message resource in Action class using getResources()..

Code:
String centrale  = getResources(request).getMessage("ambito.centrale");
 
Thanks, but my matter was just avoiding it... seems like it comes a little bit uncomfortable, anyway. Could be the best thing is to do as you say.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top