Struts Flow

The struts life cycle describes the flow of struts application .The different actions  that is performed  and the struts flow in the struts life cycle are depicted below.

Struts Life Cycle
Struts Life Cycle

Steps for request lifecycle in strut two applications:-

1)      In first step user sends a request to the server for some resource.

2)      The filterDispatcher accept the request and then it determines the appropriate action.

3)      The interceptors configured for applying the common functionalities like workflow, validation, and file upload etc and these functionalities are automatically to the request. The Interceptors are helps to specify the “request-processing lifecycle” for an action. Interceptors are configured to apply the common functionalities like workflow, validation etc to the request.

4)      The action method executed to perform the database related operations such as storing and retrieving data from the database.

5)      The result rendered the output.

6)      The request returns through the interceptors in the reverse order.  The retuning request permits us to perform the clean up or additional processing.

7)      The final step is returned to the servlet container which sends the output to the user browser.

The flow of strut application is given below.

The ActionServlet stands as a back bone of the struts applications. First the request is handled by the either doGet() or doPost() method of the Action Servlet. After that the   Action Servlet calls the process() method of its own. This method gets the Current Request Processor associated with the request. Then this request processor calls its process() method. The Request Processor also a type of Servlet. This is the actual place where the request is handled. This method reads the struts-config.xml file and find outs the Action class by seeing the request URL path.

Eg: –

Incoming request URL path is:/login
struts-config.xml:
<action path=“/login” type=“Test.LoginClass”>
<forward name=“Succes” path=“/Welcome.jsp”/>
<forward name=“failure” path=“/Login.jsp”/>
</action>

After identifying the Action class it checks the whether the request is associated with any form bean. This can be checked by using the name attribute of the action element from the struts-config.xml.

Eg: –

<form-beans>
<form name=“loginForm” type=“Test.LoginForm”/>
</form-beans>
<action-mappings>
<action path=“/login”type=“Test.LoginClass”
name=“loginForm”>
…..
</action-mappings>

Then it made the instance of ActionForm and calls the corresponding getter and setter methods for the incoming request parameters. We can validate the request parameter by calling the validate() method in ActionForm. For this we need to specify validate=“true” and input=“/Login.jsp” in struts-config.xml file. Now the actual request is handled by the Action class by calling the execute() method. Output of this method is the response of the request and the return type of this request is ActionMappings.

The browser creates a request to the struts application, the request is processed through ActionServlet. Here the ActionServlet indicates the controller of the strut application. The controller fill the ActionForm or view the object with the HTML form data and invokes it’s validate() method. The controller executes the action object.  The action object is also a controller. The action controller interfaces with model components and make data for view. The action controller forwards control to the JSP or view page. The JSP or view uses model data to generate a response to the browser.