I am new to struts & we are using jsp & struts 1.3.10 (with some actions being Spring configured too) in our project. The web app design has a separate action class for each web page and has all the action mappings as expected in the struts-config.xml. There also exists a 'BaseAction' class that extends from 'Action' in our web app and we have made all the remaining actions in our web app to extend from this base action. This BaseAction class does not have an execute method.
What I want to do
I want to include a simple java method, say, checkOfficeHours
, that checks for the office hours each time when a page is rendered. That is, I want this method to be executed each time an action is invoked.
And also this method will set a request attribute for my jsp to use.
So its a common piece of code for all the actions but I do not wish to write the same method in all the java classes. Is there a way by which I can code this method in the BaseAction class and it automatically gets executed by each action by itself, without having to explicitly call it from each action class?
Can I use the setToken(HTTPServletRequest)
method to do this? I have been searching on the internet but could not find much with struts 1.3.
Any inputs will be greatly appreciated.
Can I use a constructor in a Base ActionForm & make all the ActionForms extend from the base? Can I be sure that a constructor will be always invoked for an ActionForm?
How would a constructor not be invoked during object creation?
That said, if you're putting something into the request scope (e.g., not in a form) them consider using a custom request processor or a filter instead.
Lastly, if this is application-wide behavior then why put it in something that happens per-request (a filter or custom request processor) or even during action instantiation? Put it in a filter, check if the day has rolled over per-request (still need to do that), and put it in application scope.