Tuesday, March 1, 2011

Struts Flow-How Struts Works?

Struts Flow-How Struts Works?

Struts Flow start with ActionServlet then call to process() method of RequestProcessor.

PART I: ActionServlet

1. Any struts web application contains the ActionServlet configuration in web.xml file.

2. On load-on-startup the servlet container instantiate the ActionServlet.

3. In init() method, the ActionServlet reads the Struts Config file as an init-param and load into memory.

4. ActionServlet intercepted and processed the URL.

5. Then ActionServlet delegates the request handling to another class called RequestProcessor by invoking its process() method.

PART II: RequestProcessor

6. The RequestProcessor looks up the configuration file for the URL pattern /submitForm (if the URL is http://localhost:8080/app/submitForm.do). And finds the XML block (ActionMapping) with path attribute.

7. The RequestProcessor instantiates the form (EmpForm) on attribute name puts it in appropriate scope – either session or request. The RequestProcessor determines the appropriate scope by looking at the scope attribute in the same ActionMapping.

8. RequestProcessor iterates through the HTTP request parameters and populates the EmpForm.

9. The RequestProcessor checks for the validate attribute in the ActionMapping.
If the validate is set to true, the RequestProcessor invokes the
validate() method on the EmpForm instance.

If Validate fail the RequestProcessor looks for the input attribute and return to JSP page mentioned in input tag. If Validate pass goto next step.

10. The RequestProcessor instantiates the Action class specified in the ActionMapping (EmpAction) and invokes the execute() method on the EmpAction instance.

11. In return mapping.findForward("success") RequestProcessor looks for the success attribute and forward to JSP page mentioned in success tag. i.e. success.jsp.
In return mapping.findForward("failure")
RequestProcessor looks for the failure attribute and forward to JSP page mentioned in failure tag. i.e. failure.jsp

public ActionForward execute(

ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response) throws Exception{

//your logic
return mapping.findForward("success");

}

Signature of the execute method

No comments: