Detailed introduction to Struts 2 Architecture
Struts 2 Framework Architecture
In the previous section we learned about MVC framework and in this section
we will learn about the different components of Struts 2 framework.
How Struts 2 Framework works?
Suppose you are accessing the product list page in a Struts 2 shopping cart application and typing <your-domain/showproducts.action> to get the list of all the products available for purchase. In any Struts 2 application *.action is the default mapping configured in web.xml file. Suppose the name of the action is showpoducts.action, which retrieves the data from the model (database) and displays on the browser. In this scenario the request processing steps can be summarized into blow mentioned steps:
- The browser requests for showproducts.action.
- The Filter Dispatcher of Struts 2 framework looks at the request and determines the appropriate Action. In this case "showproducts".
- Next the Interceptors are applied. The Interceptors are used to apply common functionality to the request such as like workflow, validation, and file upload handling.
- Then the action method is executed. In our example appropriate method on the action (showproducts) is executed to retrieve the product list data from model.
- Finally the Result renders the output to the browser in the form of product list.
Following diagram shows the high level architecture of Struts 2 Framework:
Detailed Architecture of Struts 2 Framework
Following diagram shows the detailed architecture of Struts 2 Framework.
The above diagram depicts the architecture of Struts 2 Framework. Following diagram shows that the initial request goes to the servlet container, which is then passed through standard filer chain.
The filter chain includes:
1. Action ContextCleanUp filter: The ActionContextCleanUp filter is
optional and it is useful when integration has to be done with other
technologies like SiteMash Plugin.
2. FilterDispatcher: Next the FilterDispatch is called, which in turn
uses the ActionMapper to determine weather to invoke an Action. If the action is
required to be invoked, the FilterDispatcher delegates the control to the
ActionProxy.
3. ActionProxy: The ActionProxy takes the help from Configuration Files
manager, which is initialized from the struts.xml. Then the ActionProxy creates
an ActionInvocation, which implements the command pattern. The ActionInvocation
process invokes the Interceptors (if configured) and then invokes the action.
The ActionInvocation looks for proper result. Then the result is executed, which
involves the rendering of JSP or templates.
Then the Interceptors are executed again in reverse order. Finally the response
returns through the filters configured in web.xml file. If the
ActionContextCleanUp filter is configured, the FilterDispatcher does not clean
the ThreadLocal ActionContext. If the ActionContextCleanUp filter is not present
then the FilterDispatcher will cleanup all the ThreadLocals present.
Struts 2 Core components
Struts 2 Core components are Action handler, Result Handler and Custom Tags.
- Action handler
Action handler interacts with other layers - Result Handler
Result handler actually dispatches the request to view. - Custom Tags
Custom Tags are used render the dynamic content - Interceptors
Interceptors allows to add some custom logic such as authentication, file upload to an action. Interceptors code is executed before and after an Action is invoked - Value Stack
The ValueStack allows the expression language to find property values across multiple objects. Thus Struts2 tags can access data from the action very easily. - Expression Language
Struts 2 expression language can be used for getting and setting properties of Java objects
We will learn about these core components in the future lessons.