Struts 2 Architecture
Architecture of Struts 2
Struts 2 Architecture is based on WebWork 2 framework. It leverages the standard JEE technologies such as Java Filters, JavaBeans, ResourceBundles, Locales, XML etc in its architecture. Following is its framework diagram.
In Struts, there is only one controller servlet for the entire web application. This controller servlet is called ActionServlet and resides in the package org.apache.struts.action. It intercepts every client request and populates an ActionForm from the HTTP request parameters. ActionForm is a normal JavaBeans class. It has several attributes corresponding to the HTTP request parameters and getter, setter methods for those attributes. You have to create your own ActionForm for every HTTP request handled through the Struts framework by extending the org.apache.struts.action.ActionForm class. Consider the following HTTP request for App1 web application – http://localhost:8080/App1/create.do?firstName=John&lastName=Doe. The ActionForm class for this HTTP request is shown in the listing below. The class MyForm extends the org.apache.struts.action.ActionForm class and contains two attributes – firstName and lastName. It also has getter and setter methods for these attributes. For the lack of better terminology, let us coin a term to describe the classes such as ActionForm – View Data Transfer Object. View Data Transfer Object is an object that holds the data from html page and transfers it around in the web tier framework and application classes.
Here are the following steps needs to be followed to understand the flow and components of the Struts 2 framework
1. The normal lifecycle of struts begins when the first request is sent from client to the web server. This results invoke the servlet container which in turn is passed through standard filter chain.
2. The FilterDispatcher filter is called which consults the ActionMapper to determine whether anAction should be invoked.
3. If ActionMapper finds an Action to be invoked, the FilterDispatcher delegates control toActionProxy.
4. ActionProxy reads the configuration file such as struts.xml. ActionProxy creates an instance ofActionInvocation class and delegates the control.
5. ActionInvocation is responsible for command pattern implementation. It invokes the Interceptors one by one (if required) and then invoke the Action.
6. Once the Action returns, the ActionInvocation is responsible for looking up the proper result associated with the Action result code mapped in struts.xml.
7. The Interceptors are executed again in reverse order and the response is returned to the Filter (In most cases to FilterDispatcher). And the result is then sent to the servlet container which in turns send it back to client.