Struts 2 Architecture

Struts Basic Architecture:

Struts follow MVC (Model View Controller) Architecture. Following components are required for struts MVC architecture:

  1. Action
  2. Interceptor
  3. View technologies
  4. Value stack
  5. Results
Struts Architecture

In struts architecture, the controller is designed with the servlet dispatch filter and interceptors. This implementation is done with action. The view page shows the result of an application or it can be used to take input from the user. Value stack provides a common thread. It also supports the integration of various components in the architecture. Various configuration files maintain configuration information about the web application.

Struts Basic flow:

  • Some resources are requested by the user by sending a request to the server.
  • Appropriate action is then decided by Filter Dispatcher on examining the request.
  • According to the request, interceptor functionalities are applied.
  • The requested action is performed.
  • Configured interceptors are then applied to handle post-processing.
  • At last, the result is prepared and displayed to the user using view technologies.

Struts Standard flow:

  • In the first step, the User of an application request for the action.
  • The request is mapped in the web.xml file to get the associated controller class name.
  • A container such as FilterDispatcher is responsible for invoking the controller.
  • ActionMapper then provides all the information to the controller about the action.
  • ActionProxy is invoked by the controller.
  • Configuration Manager provides action and interceptor stack information to ActionProxy from the struts.xml file.
  • The request is then forwarded to ActionInvocation.
  • Each action and interceptor is invoked by ActionInvocation.
  • Finally, the result is generated and provided to ActionInvocation.
  • The response is generated with the help of HttpServletResponse and sent to the user.

Two types of Design models for struts, Model 1 and Model 2.

Model 1 architecture:

Model 1 architecture deals with the use of servlet and JSP technologies to develop web applications.

Servlet uses threads to handle the request. Thread is used instead of process. Thread shares memory area. It does not allocate separate memory areas like processes. In servlet web applications business logic and user interface designs are mixed. There is no separation of concern. It needs recompilation whenever the code is modified.

In JSP web applications, business logic is separated from presentation logic. If any of the JSP pages are modified, redeployment of application is not required. JSTL provides a rich library to develop web applications using JSP.

The flow of Model 1 architecture:

Struts Architecture

As shown in the above figure first user request a JSP page for a particular action to be performed.

JSP then approaches Java Bean to perform business logic.

Java Bean establishes a connection with the database to store or retrieve the data.

Interaction among the Java Bean and database generates the response sent to the JSP which is later displayed on the browser to the user.

Advantages of struts Model 1 architecture:

  1. It is easier to develop web applications.
  2. With Model 1 architecture, the development will be faster.

Disadvantages of struts Model 1 architecture:

  1. Model 1 architecture is best for small web applications but not good for large applications.
  2. It is very time-consuming to develop custom tags in JSP to develop web applications.
  3. There is a maintenance problem and decentralization in Model 1 architecture as every JSP page contains logic to identify the next page. If the next page name is changed, it requires a change in every page where it is used.

Model 2 architecture:

Model, View, and Controller are included in Model 2 architecture. It is used for separating UI from the main application. It makes the application easy to understand. Model does the function of handling the data of the application. The model gives response for request from view and also respond to controller instruction. The view uses scripting technologies like JSP, ASP, and PHP. The controller gives a response to input from the user. It performs business operations. The model consists of data and business logic. To represent the data, the view is used. The controller acts as an intermediate node between the model and the view. It receives input, acts, and helps to generate a final result.

Struts Architecture

Advantages of struts Model 2 architecture:

  1. Model 2 is easy to maintain.
  2. It is easy to extend and test.
  3. It supports separation of concern.
  4. Model 2 has centralized navigation control.

Disadvantages of struts Model 2 architecture:

Redeployment of application is needed whenever change is done in controller code.