OVERVIEW OF STRUTS 2

Apache Struts was introduced in May 2000 by Craig McClanahan,
with version 1.0 released in July 2001. In December 2005, it was announced that WebWork 2.2 was accepted as Apache Struts 2 ( WebWork and Struts1 combine together to develop Struts2), which  released in February 2007. The current stable release of Struts is Struts 2.3.16.3 GA / May 3, 2014.

Struts 2 is a  web application framework for creating Java  web applications. It is based on WebWork2 technology, internally Struts 2 is not an extension of Struts 1  but, it is a rebrand of WebWork version 2.2. WebWork itself is based on XWork, an open source command-pattern framework from Open Symphony.

It is highly extensible and flexible framework based on the concept of MVC 2 architecture. And it easily separate Presentation layer, Transaction and Data model on different layers.

Q. What’s a web application framework?
A. A web application framework is a piece of structural software that provides automation of common tasks of the domain as well as a built-in architectural solution that can be easily inherited by applications implemented on the framework.

Q. Why Struts?
A. Because-

  1. Takes a lot of complexity out of building your own MVC framework.
  2. Good design practice and modeling.
  3. Easy to learn and use.
  4. Feature rich, supported 3rd-party tools.
  5. Flexible and extensible.
  6. Large user community.
  7. Stable and mature.
  8. Open source.

Q. Define Struts2 framework ?
A. Struts 2 is an open-source framework that implements the Model-View-Controller (MVC) design pattern for developing Java EE web applications and extends the Java Servlet API also. It is based on WebWork2 technology. The WebWork framework add new ideas and concepts along with original Struts code.

Q. Explain Push-MVC architecture ?
A. In Push-MVC, the data i.e. Model is created and given to the view layer by the Controllers by putting it in the scoped variables such as request or session. For example Spring MVC and Struts1.

Q. Explain Pull-MVC architecture ? Which architecture does Struts2 follow ?
A. In Pull-MVC, the data i.e. Model created in Controllers are kept in a common place i.e. in actions, which then gets rendered by view layer. For example Struts2. The Struts 2 is a Pull-MVC  (or MVC2) based architecture, in which the data is stored in Value Stack and retrieved by the view layer for rendering.

Q. How many types of design models are there?
A. The Java web applications build upon two design models –

  1. Model 1 .
  2. Model 2 (MVC) .

Model 1
Model 1 is the first design model . In Model 1 a request is made to a JSP or servlet and then that JSP or servlet handles all responsibilities for the request, Navigation from one JSP to another is done by clicking a link on the page.

 Problems with  the Model 1
The  Model 1 is page-centric and applications have a sequence or list of JSPs so the user move from one page to another. Commonly used in smaller, simple task applications but not for large applications. The Model 1 applications is tough to manage. The architecture does not support the division of labor between the page designer and the web developer.

Model 1 is not recommended for the following given reasons:

  • Navigation problem arises on changing the name of a JSP that is referred by other pages. So change the name in many locations is mandatory.
  • More use of scriptlets in JSPs because JavaBeans are limited and custom tags are hard to write.
  • To write the Java code in JSPs is time consuming in comparison to  write Java code in Java classes.

Model 2
Model 2 is the second design model  . This is the recommended architecture. Model 2 is another name for the Model-View-Controller (MVC) design pattern. In  the Model 2, there are three main constituents  : the model, the view, and the controller.

  1. Model- The model contain the application data and business logic.
  2. View- The view takes care of the display of the application.
  3. Controller- The controller accept user input and commands  the model and/or the view to change respectively.

Model 2 is recommended for the following given reasons:

  • More rapid to build.
  • Easy to test and maintain.
  •  Easy to extend.

Model 2 is suggested for medium- and large-sized applications.

Q. Explain Struts 2 MVC pattern ?
A. The MVC pattern provides a separation of concerns that means it allows us to manage the complexity of large software systems by dividing them into high-level components.
There are three main components in an application: the model, the view, and the controller.

In Struts 2, these are implemented by the action, result, and FilterDispatcher respectively.

CONTROLLER—FILTERDISPATCHER
FilterDispatcher play the role of the controller in Struts 2. This
object is a servlet filter that check each incoming request to determine which Struts 2 action should handle the request. The framework handles all of the controller work. This is achieved  by the XML-based configuration files or Java annotations.

Model-Action
The Struts 2 action component implement the model. The model contains all the stuff of the application and the model is the internal state of the application. This state is composed of both the data model and the business logic.

The controller, after receiving the request, must consult its mappings and determine which actions handle the which request.
The framework handles all of the controller work and prepare the necessary data and execute the action’s business logic. When the action completes its work, will forward the result to the Struts 2 view component.

VIEW—RESULT
The presentation component of the MVC pattern is the view. The result returns the page to the web browser. This page is the user interface that presents a representation of the application’s state to the user such as JSP pages, Velocity templates, or some other presentation-layer technology.

The role of the view is that- it translates the state of the application into a visual presentation with which the user can interact with the clients and Ajax applications.