Struts Flow-How Struts Works

A typical struts web.xml file is mentioned below.


After developing and deploying Struts application on the server, when you start the server following things will happen.

Container initializes(read the data) web.xml file.When container is reading the data from web.xml It will check all the Servlets configured. If container contains <load-on-startup> tag then it will try to load the Servlet into main memory .In our case we configured only one Servlet called ActionServlet with <load-on-startup> tag, because of this container loads the ActionServlet into the memory.Servlet container Instantiate the ActionServlet and then init() method will be called.

Inside the init() method there is logic using SAX parser to read the data from struts configuration file.Once init() method is completed , all the data from struts configuration file will be loaded into main memory. If any problem happened while passing the xml documents error will be located/reported to log file and may not allow tomcat startup.

If the user types http://localhost:8080/app/ in the browser URL bar, the URL will be intercepted and processed by the ActionServlet since the URL has a pattern *.do, with a suffix of "do". Because servlet-mapping is

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

The RequestProcessor does the following in its process() method:

ActionMapping from struts-config.xml

<action path="/submitForm"
<forward name="success"
<forward name="failure" path="failure.jsp" />
<form-bean name="EmpForm" type="com.example.form.LoginForm"/>

The RequestProcessor looks up the configuration file for the URL pattern /submitForm (if the URL is http://localhost:8080/app/

If the mapping action is not found,then the error message will be displayed to the client.

If the matching action is found then its "name" attribute value will be taken.

With that name RP (request processor) verifies all the form-bean configured to check any form bean tag name is mapping with that name. If no names is matching with the given name then error message called "cannot retrieve definition for from bean LoginForm" will be displayed to the client.

If it(Form-Bean definition) is found then its "type" attribute value will be taken which is nothing but Form Bean java class fully qualified name. Form Bean java class will be verified if it is not found then error message will be reported.

If the class is found then RequestProcessor instantiates that class and 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.

RequestProcessor iterates through the HTTP request parameters and populates the FormBean.

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. This is the method where you can put all the html form data validations.

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.

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

Signature of the execute method is:
public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception
//your logic
return mapping.findForward("success");

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

comments powered by Disqus