Navigation Bar

Saturday, December 12, 2009

Struts 2 : How Struts2 works

 Struts 2 is an MVC open source web application used for developing Java EE web applications.




The working of Struts 2 can be best illustrated with the help of a flow diagram.

From the flow diagram above we can see

  • When the web application is deployed in a web server like tomcat or JBOSS, and the web server is started, the container reads the web.xml deployment descriptor for each deployed web application.
  • In that file, you typically define the StrutsPrepareAndExecuteFilter which is mapped to a URL, typically /* meaning it will handle all incoming requests. 
  • And if the request pattern matches that in the web.xml, it calls this Filter dispatcher (StrutsPrepareAndExecuteFilter). In struts 2 the FilterDispatcher is the Front Controller,  
  • Once the web.xml configuration is loaded into memory on startup by the servlet FilterDispatcher, it triggers the loading and processing of the other configuration files also like the struts.xml, struts-default.xml and struts-plugin.xml. The servlet uses a Dispatcher class internally to manage this configuration loading process, building an internal map of actions, results, interceptors and other settings.
  • Once the web server is started and the initialization of classes and loading of configuration into memory is completed, the filter intercepts incoming HTTP requests that match its URL pattern.  These filters or interceptors handle operations like validations, file uploads, parameter binding, exception handling. These are pieces of code which get executed before and after the Action class is invoked.
  • For each relevant request, it uses the corresponding pre loaded struts.xml file to determine which specific Action class to invoke.

        Example - for the getquote example it will check in <CATALINA_HOME>/webapps/getquote/WEB-INF/web.xml file.

Sample code in the web.xml is below. In the below configuration, it calls the filterdispatcher (org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter) for all patterns passed in the url.

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Sample configuration from struts.xml for getquote web application.
<struts>
    <constant name="struts.ui.theme" value="simple" />
    <package name="default" extends="struts-default">
        <action name="lookup" class="src.LookupAction">
            <result name="success">/quote.jsp</result>
			<result name="failure">/quote.jsp</result>
        </action>
    </package>
</struts>
  • Before this Action class is executed, the request is passed through the interceptors.
  • The execute method of the Action class is executed. Here it can call functions and methods which implement the business logic and connect to the database. 
  • The data from this processed logic is then sent back to the Action class or the controller.
  • The controller then identifies the view which will render this processed data.
  • This processed data is sent through the interceptors before the response is displayed on the appropriate view.

No comments:

Post a Comment