Monday 30 April 2007

Struts Request Processing illustration, the first sight...

The following Figure shows how a request flows through the Struts framework. Follow step by step....

Step 1. The request calls the ActionServlet with a certain logical input form name.

Step 2. This logical form name is then resolved according to mapping entries defined in action.xml. This file contains the mapping that defines which Struts components are associated with which logical form.

Once the mapping is resolved, the ActionServlet will look in the session for the ActionForm class associated with the request. If such a class is located in the session, the ActionForm attributes are populated by calling the corresponding setter methods. However, if the ActionForm is not located in the session, a new instance is created and added to the user’s session. Subsequently, the reset method is called on the action form and the ActionForm attributes are populated.

Step 3/4. Next, the ActionServlet calls the validate method on the ActionForm. The validate method returns either null (indicating success) or an array of errors (indicating an error condition). If the ActionForm’s validate method returns an array of errors, the ActionServlet populates the requesting input form and displays the errors to the user. we can disable the call to this method if we define validate=false in struts config file. we can disable the validate for all the action forms if we define init parameter validate with value false in web.xml for ActionServlet(Controller).

Step 5. Once the validate method returns null, the ActionServlet will instantiate an instance of ActionClass (if one does not already exist) as indicated by the mapping in action.xml. The ActionClass execute method is then invoked by ActionServlet. The execute method is responsible for processing the request.

Step 6. The execute method is required to return an ActionForward instance. This instance indicates to the ActionServlet which JSP (if any) to forward the request off to for rendering of the presentation.

Step 7. The JSP is responsible for generating the appropriate response based on the results of executing the execute method. In Struts, an ActionForm bean has only one purpose -- to represent the last input state of all the user's entries on a particular form. the ActionForms are also called the transfer objects between the controller and the Views because they carry the values from controller (Action) to the Views (Jsp).
That way, the framework can easily redisplay the last input screen (plus some error messages) pre-populated with what the user last entered, which is what anyone who has ever used a GUI application expects.

May be the request processing seems to be complex at first sight, but once you are acquinted with it...it would put you one the drive!!!!. Enjoy Struts!!!!!

0 comments: