Introduction to Struts 2
Apache Struts is a free, open-source, MVC framework for creating elegant, modern Java web applications. It favors convention over configuration, is extensible using a plugin architecture, and ships with plugins to support REST, AJAX and JSON.
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.
Web applications differ from conventional websites in that web applications can create a dynamic response. Many websites deliver only static pages. A web application can interact with databases and business logic engines to customize a response. Web applications based on JavaServer Pages sometimes commingle database code, page design code, and control flow code. In practice, we find that unless these concerns are separated, larger applications become difficult to maintain.
One way to separate concerns in a software application is to use a Model-View-Controller (MVC) architecture. The Model represents the business or database code, the View represents the page design code, and the Controller represents the navigational code. The Struts 2 framework is designed to help developers create web applications that utilize a MVC architecture.
The Struts 2 framework provides three key components:
A “request” handler provided by the application developer that is mapped to a standard URI. A “response” handler that transfers control to another resource which completes the response. A tag library that helps developers create interactive form-based applications with server pages. Struts works well with conventional REST applications and with technologies like SOAP and AJAX.
Architecture of Struts 2:-
The diagram describes the framework’s architecture.
1- In the diagram, an initial request goes to the Servlet container which is passed through a standard filter chain.
2- The required FilterDispatcher filter is called, which consults the ActionMapper to determine if the request should invoke an action.
3- If ActionMapper determines that an Action should be invoked, the FilterDispatcher delegates control to ActionProxy.
4- The ActionProxy consults the framework Configuration Files manager (initialized from the struts.xml file). The ActionProxy creates an ActionInvocation.
5- ActionInvocation is responsible for command pattern implementation. This invokes any Interceptors one by one (if required) and then invoke the Action itself.
6- Once the Action returns, the ActionInvocation is responsible for looking up the proper result associated with the Action result code mapped in struts.xml.
7- The result is then executed, which often (but not always, as is the case for Action Chaining) involves a template written in JSP or FreeMarker to be rendered. While rendering, the templates can use the Struts Tags provided by the framework. Some of those components will work with the ActionMapper to render proper URLs for additional requests.