dcsimg
Extending Action class
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Gianluca_Gillini
Posted On:   Monday, December 20, 2004 09:19 AM

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 an   More>>

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!

   <<Less
About | Sitemap | Contact