On 11/28/06, Tarek Nabil <[EMAIL PROTECTED]> wrote:
Hi, One of the problems with Struts was that if you had a page that requires some setup and this page submits to an Action, then you would not be able to set validate to true on that action because if validation problems occur then Struts will take you directly to the input JSP without performing the setup and your JSP wouldn't work. A solution to that would be to specify the input of the action as the setup action, which means you're doing action chaining and Struts is not good at that (it will reset your form among other things). The solution of choice for us so far was to call validate() ourselves in the action and if a validation problem occurs then we call the setup method (eventually you start using your action methods as an API which still was not good). Another problematic scenario is the case when you're editing some database record for example. When you go to your setup action for the first time, you will populate drop down lists for example, and then load the existing values from the database to your JSP fields. If the user attempts to save and some problem occurs then in the setup action, you will need to again populate the drop down lists, but you will NOT want to overwrite the user's inputs with the existing data. This is very similar to the first problem, but you would also need to pass some flag to the setup method to tell it whether to copy the data from the database to your ActionForm. Does Struts 2 or even WW 2.2.4 solve that problem in an elegant way? I remember coming across something about using Interceptors for that, but that would mean creating an Interceptor for almost every action of that type. I also noticed that there are some warnings in the documentation against using action chaining in Struts 2 as well.
Interceptors are an O-O oriented approach to this kind of issue, but there are ligther weight alternatives. For the particular need of a "setup" action, consider the use of the Preparable interface in Struts 2. It's pretty similar to what the Tiles "Controller" interface supported in Struts 1.x ... a chance for your business logic to set up the stuff that is needed for rendering a particular view. If you're looking at JSF as a view technology, you'll likely be interested in what Shale <http://shale.apache.org/"> has to offer, particularly in the View Controller feature. Not only does the View Controller functionality support the setup scenario (via the prerender() callback method on the ViewController interface), it also supports cleaning up *after* the view has been rendered, via the destroy() callback. Craig