How Struts 2 Works
Apache Struts 2 is a free open-source MVC web application framework for developing Java EE web applications. This tutorial explains how struts 2 works.
Working and flow of Struts 2
- The request is first sent from the browser to the server. Then the server loads the web.xml and if the request pattern matches then it is forwarded to the FilterDispatcher. In struts 2 the FilterDispatcher is the Front Controller.
- Based on the request url and it’s mapping in the struts.xml the appropriate action class to be executed is decided.
- Before the Action class is executed the request is passed through the interceptors.
- The action method of the action class (controller) is executed.
- The action class calls the business logic function.
- The Business Logic class works with the database.
- Bushiness logic class gets the data from the database.
- Processed data from bushiness logic is sent back to the Action class or the controller.
- Depending on the result the Controller identifies the view to be rendered.
- Before the response is generated the interceptors are executed again.
Configuring the FilterDispatcher
In Struts 2 a filter called FilterDispatcher is used as the Front Controller. To use the FilterDispatcher it has to be registered in the deployment descriptor (web.xml) as below.
Struts splits the task processing in its filter dispatcher into subcomponents called interceptors. Eg the work of the first interceptor is to populate the action object with the request parameters.
In Struts action method is executed after the the action’s properties are populated.
The method in the action returns a String. Eg if it returns a “Success” , or “failure”, depending upon the return value it can be forwarded to the result page. The forwarded result may not always be a jsp, it can even be a file to be downloaded.
Working of the Filter Dispatcher
The work of the Filter Dispatcher is to first verify the request URI and determine which action to invoke. Struts uses the configration file struts.xml which matches URIs with action classes. One has to create a struts.xml and put it in the WEB-INF/classes folder. In this xml file the action definitions,concrete action class name, URI to invoke the action should be placed. If the name of the method in the action is other then execute then it should be also mentioned in the web.xml.
The action class should have at least one result to tell what to do after the action method is executed. It can have multiple results if the action method returns different results depending upon the user input.
When struts starts it reads the struts.xml. When ever struts gets a request it checks the timestamp of the struts.xm. Itf it is recent then when it was last time loaded then it is reloaded, thereby if one makes changes to the struts.xml, restarting the server is not required.
During each action invocation the filter dispatcher does the following steps.
- It consults the Configuration Manager to find out which action to execute depending on the request URI.
- Invoke the interceptors registered with this action. First interceptor will populate the properties of the action.
- Executes the action method.
- Executes the result.