Pages Navigation Menu

Coding is much easier than you think

Architecture overview of Struts 2 and how it works

Posted by on Apr 18, 2013 in Struts 2 Tutorial | 12 comments

Struts Execution Flow Diagram


The article Concept of Servlets Vs Concept of Struts 2 helps you in understanding the concepts of struts 2 better.

Execution flow of struts2
The flow starts with the web browser placing a request for a particular resource. This request is received by the web container. The container loads web.xml and checks if the url patterns match. If it matches, the web container transfers the request to Filter Dispatcher.

The Filter Dispatcher decides the suitable action , by calling the ActionMapper , which in turn calls the ActionProxy. ActionProxy reads the configuration file such as struts.xml, and finds the suitable action for the request. ActionProxy creates an instance of ActionInvocation class. The ActionInvocation class invokes the Interceptors one by one (if required).
Do Read

  • Concept of Interceptors in Struts 2
    The Interceptors use the required functions and after that, the Action method executes all the functions like storing and retrieving data from a database. ActionInvocation receives the final result produced by an action class.

    Once the Action returns, the ActionInvocation is responsible for looking up the proper result associated with the Action result code mapped in struts.xml. The Interceptors are executed again in reverse order and the response is returned from ActionProxy to the Filter (In most cases to FilterDispatcher). FilterDispatcher selects an appropriate view, basing on the result. The result is then sent to the servlet container which in turn sends it back to the client. Then the result can be seen on the output of the browser in HTML, PDF, images or any other format.

    The other important features of Struts 2 are ValueStack & OGNL. Object-Graph Navigation Language (OGNL) is a powerful expression language that is used to reference and manipulate data on the ValueStack. OGNL expression language provides simplified syntax to reference java objects. It helps in data transfer and type conversion by binding the java-side data properties to the string-based view layer.

    Action context
    ** UPDATE: Struts 2 Complete tutorial now available here.
    In Struts 2 the action resides on the ValueStack which is a part of the ActionContext. ActionContext is a global storage area that holds all the data associated with the processing of a request.

    When a request comes the param interceptor helps in moving the request data to the ValueStack. Now the OGNL does the job of converting the string based form data to their corresponding java types. OGNL does this by using the set of available built-in type converters.

    Again when the results are generated the OGNL converts the java types of the property on the ValueStack to the string-based HTML output.Here for N number of request N number of object is created, this makes the Struts 2 actions thread safe.
    The article Servlets Vs Concept of Struts 2 helps you in understanding the concepts of struts 2 better.

    Read More