Pages Navigation Menu

Coding is much easier than you think

Architecture overview of Struts 2 and how it works

Architecture overview of Struts 2 and how it works

 
Struts Execution Flow Diagram
 

Diagram_tmp

 
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.
     

    About Mohaideen Jamil


    Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

    • ASSILA Anas

      thank you amazing tutorial

    • Binh Thanh Nguyen

      Thanks, nice explanation

      • http://www.simplecodestuffs.com/ Mohaideen Jamil

        Most welcome :)

    • http://gravatar.com/radhakrishna80 radhakrishna80a

      Hi Jamil,

      I am facing an issue in Struts2 validation. I have a form with 2 actions one of them is Ajax and another is normal submit. I have implemented validations and its working fine but, when validation fails form is keeping old data in the request for all subsequent actions. Even though I have corrected data after validation errors, its still throwing validation errors as the form is retaining old data. Can you please help me in this issue

    • http://gravatar.com/nazwapullarat Jusaila

      HI Jamil

      I Have Some Doubt on Struts 2

      1)what is the need or why we are using Struts 2 in our Projects

      2)What are the benefits we will get if we use Struts 2 Frame work in Our projects

      3)How struts 2 differ from the other Frame works

    • Manjunath

      Need some webservices tutorials SOAP

    • http://zaheer.basha@gmail.com zaheer

      hi this is zaheer,….plz help me .can u send me any search source code example using struts2 application+mysql database……

      • http://www.simplecodestuffs.com Mohaideen Jamil

        Hi Zaheer,

        Do you need Struts + MySql integration example or Code for some other scenario ?

        You can refer this page for more example on Struts 2

        Struts 2 Tutorial

    • SaiKiran Marripati

      how to contact u jamil… u can mail me at saikiranm@rocketmail.com

      • Jamil

        Hi SaiKiran,
        You can contact me through this blog or through facebook fan page :)

        • http://gravatar.com/saikiranmarripati SaiKiran Marripati

          Thank you