Apache Struts2 was originally known as WebWork 2. After working independently for several years, the WebWork and Struts communities joined forces to create Struts2. This new version of Struts is simpler to use and closer to how Struts was always meant to be.
Struts 2 is a pull-MVC framework. i.e. the data that is to be displayed to user has to be pulled from the Action.
Struts2 supports annotation based configurations which are easy to create and more intuitive. Action class in Struts 2 act as the model in the web application. Unlike Struts, Struts 2 Action class are plain POJO objects thus simplifying the testing of the code. Struts2 also comes with power APIs to configure Interceptors that reduce greatly the coupling in application. The view part of Struts 2 is highly configurable and it supports different result-types such as Velocity, FreeMarker, JSP, etc.
MVC Framework Struts vs MVC pattern:
- Pattern is the way you can architect your application.
- Framework provides foundation of classes and Libraries.
- Framework gets us started quickly.
MVC : Model View Control, am giving real time definitions, i don’t want to confuse you by saying lines of definitions
Model: Will concentrates on business logic of an application, am going to take bean as model.
View: will take cares the presentation, i mean visible part, we are going to use jsp as view for these struts 2 applications. The view part provides interface with the outside world to our application. A single model can have multiple view for different purposes. The view object defines the way how the data is to be presented. View object is responsible for maintaining the consistency in data presentation whenever the model changes.
Controller: is a component, which contains all primary logic’s required, like sessions and security and also it contains the entire flow of applications, we are going to use servlet as controller for these struts 2 applications. The controller controls the user interaction between the view and model. The controller does not includes any business logic. It also strictly deals with exposing of methods included in the model to the user through the view. The controller controls the updation of a particular parameter in the view which is then displayed by a view. Based on the outcome of the interaction with the user and model, the controller determines the appropriate view.
Building our own MVC framework:
To building our own MVC Framework, there is a client call the handler to handle parameters. Handler is like a controller it is routed by the route configuration file, this file has all the configuration for routing the specific controller and services. And another level is the Model classes, these are the business classes , in these define all the business logic these logic apply to the request parameters and passes to view module, view module has different tag libraries, please see in the below pics.
Struts 2 Follow MVC pattern:
All requests passes through interceptors and struts 2 configuration is take care about all request and interact to specific action class and action class return the result to view jsp file.
The Flow of Struts 2 based Application
Here is the Request Lifecycle in Struts 2 applications:
- User Sends request: User sends a request to the server for some resource.
- FilterDispatcher determines the appropriate action: The FilterDispatcher looks at the request and then determines the appropriate Action.
- Interceptors are applied: Interceptors configured for applying the common functionalities such as workflow, validation, file upload etc. are automatically applied to the request.
- Execution of Action: Then the action method is executed to perform the database related operations like storing or retrieving data from the database.
- Output rendering: Then the Result renders the output.
- Return of Request: Then the request returns through the interceptors in the reverse order. The returning request allows us to perform the clean-up or additional processing.
- Display the result to user: Finally the control is returned to the servlet container, which sends the output to the user browser.