Stay Connected  Follow abhisays on Twitter   Connect to abhisays on Facebook  

Last Updated: September 27, 2014  1,693 views

What is Struts?

in: Technology

Struts GuideToday I am going to discuss a top level Apache project, Struts. It is an open-source web application framework for developing Java EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt a model-view-controller (MVC) architecture. It was first created by Craig McClanahan and donated to the Apache Foundation in 2000 and it became famous with the name Apache Struts. It is generally used for building web applications based on the servlet and JSP (Java Server Pages) technologies. But before going into the details of Struts, let’s talk about MVC.

Model-View-Controller (MVC) is an architectural pattern used in software engineering. The main aim of MVC is to isolate business logic from input and presentation, permitting independent development, testing and maintenance of each layer.

Introduction of Servlets and JSPs completely revolutionized the process of web development, but there were some limitations in simply using both technologies together. Servlets are Java classes that implement the javax.servlet.Servlet interface. They are compiled and deployed in the web server. The problem with servlets is that you embed HTML in Java code. If you want to modify the cosmetic look of the page or you want to modify the structure of the page, you have to change code, which is not a good practice. However, if you can use JSP and write all your HTML codes there, moreover, you can embed Java code into the jsp. But too much java code in jsp is also not good for the health of web application. Servlets are great for Java coding. JSPs are great for HTML developers. Placing HTML into servlets or placing Java code in JSP leads to a system that is very hard to maintain and customize. This brings us to the Model 1 architecture or MVC 1.

Model — Model represents enterprise data and the business rules that govern access to and updates of this data. Model is not aware about the presentation data and how that data will be displayed to the browser.

View — View represents the presentation layer of the application. The view object refers to the model. It uses the query methods of the model to obtain the contents and renders it. The view is not dependent on the application logic. It remains same if there is any modification in the business logic.

Controller — Whenever the user sends a request for something then it always go through the controller. The controller is responsible for intercepting the requests from view and passes it to the model for the appropriate action. After the action has been taken on the data, the controller is responsible for directing the appropriate view to the user.

MVC1 is actually a page centric Architecture. In MVC1, there is no clear distinction between view and a controller. There is JSP page (view and partial controller) which itself controls Business logic (Model) that is why it is also called as page-centric architecture. JSP files are used to code the presentation. To retrieve the data JavaBean can be used.


In MVC2 we have a clear separation of view and controller. A controller receives all the requests, retrieves data from a Model and forwards it to next view for presentation. There is a central Servlet (Controller) which calls business logic (Model) and the forwards it particular JSP page (View). So MVC2 is also called servlet-centric architecture. The MVC2 architecture removes the page centric property of MVC1 architecture by separating Presentation, control logic and data. There is only one controller which receives all the request for the application and is responsible for taking appropriate action in response to each request.


But MVC2 is also not the final solution for developing powerful web applications using Servlets and JSPs. If we code a separate controller servlet for each page, we can end up with dozens of almost identical servlets. If we code a single controller servlet, we end up with a huge monolithic class with large number of if/else statement that can be difficult to maintain. What’s needed to solve this problem is a controller architecture that can handle the repetitive details while still remaining flexible and easy to maintain. Now let’s come to Struts. It is an open source web application framework that provides a flexible and easy to use controller architecture. Struts helps developers to use MVC2 web application architecture with clear separation of model, view and controller. Struts includes a controller servlet, JSP custom tag libraries, and utility classes.

Struts Architecture

Struts Flow ::

StrutsThe user requests a html form, then this form is submitted to the URL of the form That address is mapped by struts-config.xml to an action class. The execute method of the action class is invoked. One of the arguments to execute is form bean that is automatically created and whose properties are automatically populated with the incoming form data. The Action object then invokes business logic and data-access logic, placing the results in normal beans stored in the request session, or application scope. The Action uses mapping . findForward to return a condition and conditions are mapped by struts-config.xml to various JSP pages. In this way, Struts forward the request to the appropriate JSP page.

What is Controller in Struts Application?

Struts provides the Controller portion of the web application. The Controller receives requests from the client (user running a web browser), deciding what business logic function is to be performed, and then delegating responsibility for producing the next phase of the user interface to an appropriate View component.

There are three components of Controller

  1. Action Servlet (It is provided by the Struts Framework)
  2. Request Processor (It is also provided by the Struts Framework)
  3. Action Classes (They are written by the web application developer)

org.apache.struts.action.ActionServlet Class is a simple servlet with doGet( ) and doPost( ) methods.

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
process(request, response);

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
process(request, response);

User of ActionServlet in Struts Framework ::

When ActionServlet receives a request from client, the doPost( ) or doGet( ) methods receive a request and invoke the process( ) method. This method gets the current RequestProcessor and invokes the RequestProcessor. The RequestProcessor.process( ) method is where the current request is actually serviced. This method matches <action> element in struts-config.xml with the path in the html form. If matches, then it looks for <form-bean> entry that has a name attribute that matches the <action> element’s name attribute. Now, the RequestProcessor.process( ) method knows the full name of the FormBean, it creates or retrieves a pooled instance of the ActionForm named by the <form-bean> element’s type attribute and populates its data members with the values submitted to the request. Once the ActionForm’s data members are populated, the RequestProcessor.process( ) method calles the ActionForm.validate( ) method, which checks the validity fo the submitted values.

At this point RequestProcessor.process( ) method knows all that is needs to know and it is actually service the request. It does this by retrieving the fully qualified name of the Action class fromt he <action> element’s type attribute, creating or retrieving the name class and calling the Action.execute( ) method. When the Action class returns from its processing, its execute( ) methods returns an ActionForward object that is used to determine the target of this transaction. The RequestProcessor.process( ) method resumes control, and the request is then forwarded to the determined target. Now the ActionServlet instance has completed its processing for this request and is ready to service future requests.

org.apache.struts.action.Action class —

The Action class extends the org.apache.struts.action.Action class, implements the appropriate execute( ) method. You can add your business logic in Action class. Also, you need to add <action> element to the application’s struts-config.xml files describing the new Action.

Signature of Action.execute( ) method —

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException

ActionMapping contains all the deployment information for a particualr Action bean. ActionForm represents the Form inputs containing the request parameters from the View referencing the Action bean.

Struts Summary —

The goal of Struts is to separate the model (application logic that interacts with a database) from the view (HTML pages presented to the client) and the controller (instance that passes information between view and model). Struts provides the controller (a servlet known as ActionServlet) and facilitates the writing of templates for the view or presentation layer (typically in JSP, but XML/XSLT and Velocity are also supported). The web application programmer is responsible for writing the model code, and for creating a central configuration file struts-config.xml that binds together model, view and controller.

Requests from the client are sent to the controller in the form of “Actions” defined in the configuration file; if the controller receives such a request it calls the corresponding Action class that interacts with the application-specific model code. The model code returns an “ActionForward”, a string telling the controller that output page to send to the client. Information is passed between model and view in the form of special JavaBeans. A powerful custom tag library allows it to read and write the content of these beans from the presentation layer without the need for any embedded Java code.

Struts also supports internationalization, provides facilities for the validation of data submitted by web forms, and includes a template mechanism called “Tiles” that (for instance) allows the presentation layer to be composed from independent header, footer, and content components.

  • 1

    nice indepth article..

    Mohit on November 25th, 2009
  • 2

    This is excellent, but I guess struts is being replaced by spring now.

    iresha on November 25th, 2009
  • 3

    Struts MVC Architecture
    The model contains the business logic and interact with the persistance storage to store, retrive and manipulate data.
    The view is responsible for dispalying the results back to the user. In Struts the view layer is implemented using JSP.
    The controller handles all the request from the user and selects the appropriate view to return. In Sruts the controller’s job is done by the ActionServlet.

    tea on November 26th, 2009
  • 4

    What a topic! I love this article.

    Essays on November 26th, 2009
  • 5


    its really indepth article for beginner …..
    Means Its nice for me ;)..

    finally i did my first comment on your article as I am regular visitor of your blog for more than 6 months.

    Ashvin Kumar Malviya on November 27th, 2009
  • 6

    Yes , I like it,great,thanks a lot

    juicy couture on March 31st, 2010
  • 7

    What explaination given by you ,it helps all who is learner at initial step well learn after red this article.
    thank you much Sir,

    `GAURAV PATIDAR on October 13th, 2012