Explain the Struts1/Struts2/MVC application architecture?

Struts was adopted by the Java developer community as a default web framework for developing web applications

The MVC(Model–view–controller) an application that consist of three distinct parts. The problem domain is represented by the Model. The output to the user is represented by the View. And, the input from the user is represented by Controller.

In Core Java, Struts1 and Struts2 are both web application frameworks that follow the Model-View-Controller (MVC) architecture. However, there are significant differences between Struts1 and Struts2 in terms of design, features, and improvements.

Struts1 MVC Architecture:

  1. Model (ActionForm):
    • In Struts1, the model is represented by ActionForm. ActionForm is a JavaBean that holds the form data submitted by the user.
    • It acts as a container for user input and is responsible for validating the data.
  2. View (JSP):
    • JSP (JavaServer Pages) is used as the view component in Struts1.
    • The JSP pages display the data and interact with the user.
  3. Controller (ActionServlet):
    • The controller in Struts1 is represented by the ActionServlet.
    • ActionServlet is responsible for handling the incoming requests, invoking the appropriate Action class, and managing the flow of control.
  4. Action:
    • Action classes encapsulate the application logic.
    • They are responsible for processing the requests, interacting with the model (ActionForm), and forwarding the request to the appropriate view (JSP).

Struts2 MVC Architecture:

  1. Model (Action Classes and ValueStack):
    • In Struts2, the model is represented by action classes and the ValueStack.
    • Action classes are POJOs (Plain Old Java Objects) that contain the application logic.
    • The ValueStack holds the data and is accessible by the views.
  2. View (JSP, FreeMarker, etc.):
    • Struts2 supports multiple view technologies, including JSP, FreeMarker, and others.
    • The views are responsible for rendering the data from the ValueStack.
  3. Controller (ActionDispatcher and Struts Filter):
    • The controller in Struts2 is managed by the ActionDispatcher and the Struts filter.
    • The filter is responsible for intercepting requests and dispatching them to the appropriate action.
    • Interceptors provide additional functionality and can be configured to execute before and after the action.
  4. Struts2 Interceptors:
    • Interceptors in Struts2 provide a way to intercept the request processing flow.
    • They can be used for tasks such as validation, authentication, logging, etc.

Key Differences:

  • Struts2 is considered more flexible and extensible compared to Struts1.
  • Struts2 uses a more modern approach with action classes as POJOs, while Struts1 relies on extending specific base classes.
  • Struts2 uses a ValueStack to manage the data, providing better separation of concerns.
  • Struts2 supports various view technologies, giving developers more options.

In summary, while both Struts1 and Struts2 follow the MVC architecture, Struts2 introduces improvements and a more modern approach, making it a preferred choice for new Java web applications.