• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar

JavaTutorOnline

Java Tutor Online

  • Home
  • Courses
  • Tutorials
    • Java
    • Servlets
    • Struts
    • Spring
    • Webservice
  • FAQ
  • Testimonials
  • Blog
  • Contact Us

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.

How Struts 2 Works Flow Diagram

Working and flow of Struts 2

  1. 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.
  2. Based on the request url and it’s mapping in the struts.xml the appropriate action class to be executed is decided.
  3. Before the Action class is executed the request is passed through the interceptors.
  4. The action method of the action class (controller)  is executed.
  5. The action class calls the business logic function.
  6. The Business Logic class works with the database.
  7. Bushiness logic class gets the data  from the database.
  8. Processed data from bushiness logic is sent back to the Action class or the controller.
  9. Depending on the result the Controller identifies the view to be rendered.
  10. Before the response is  generated the interceptors are executed again.

Learn Java, J2ee, Struts, Spring by Private Online Java Tutor

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.

1
2
3
4
5
6
7
8
9
10
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
<filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-patter>
</filter-mapping>

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.

  1. It consults the Configuration Manager to find out which action to execute depending on the request URI.
  2. Invoke the interceptors registered with this action. First interceptor will populate the properties of the action.
  3. Executes the action method.
  4. Executes the result.
How Struts2 Works

Filed Under: Struts

Reader Interactions

Comments

  1. Deepak says

    April 21, 2014 at 5:27 pm

    What is the use of interceptors

    Reply
    • javatution says

      April 26, 2014 at 6:55 am

      Interceptors allow for crosscutting functionality to be implemented separately from the action.

      Interceptors are used in providing preprocessing logic before the action is called or postprocessing logic after the action is called.

      Reply
      • Gagan Bansal says

        June 11, 2014 at 11:06 am

        thanks for posting this article,
        I have a confusion regarding Interceptors , May you tell me about interceptors by taking an example.

        Reply
      • Manu says

        December 11, 2014 at 1:02 pm

        🙂

        Reply
  2. Abirami K says

    June 3, 2014 at 7:33 am

    could you explain interceptors in detail

    Reply
  3. Rajen says

    July 20, 2014 at 1:38 pm

    What is the work of Action Proxy and Action Mapper in struts2?

    Reply
  4. manoranjan says

    October 21, 2014 at 7:02 am

    can u elaborate it with an example…

    Reply
  5. abhishek says

    January 10, 2015 at 5:46 pm

    sir which type of cross functionality provids interceptor

    Reply
  6. Rajesh says

    September 21, 2015 at 3:07 am

    If the name of the method in the action is other then execute then it should be also mentioned in the web.xml.

    I think we have to mention in struts.xml. RIght

    Reply
  7. Amine says

    September 3, 2019 at 8:17 am

    Very helpful … Thank you very much for this post, now I understand better how Struts work.

    Reply
  8. Koushika says

    August 13, 2024 at 5:21 pm

    THank you so much

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Mr Chinmay

Chinmay Patel
Online Java Tutor-Demo Class

Phone & Whatsapp +919853166385
[email protected]

Recent Posts

  • How to Learn Java in One day? | JavaTutorOnline
  • Constructor in Java and Overloaded Constructor Example Program
  • Important Interview Questions on Java Multithreading
  • React Spring Boot Web Services Integration
  • Spring Boot RESTful Web Services Example
  • Top Spring MVC Interview Questions and Answers for Developers
  • Top Spring Core Interview Questions and Answers for Developers
  • Host Java Web Apps for Free on Mobile with Tomcat and Termux
  • How to Deploy Java Web Application on Aws EC2 with Elastic IP
  • Simple Jsp Servlet Jdbc User Registration using Tomcat Mysql and Eclipse
Copyright © 2025 JavaTutorOnline