Thursday, September 6, 2018

Introduction to Struts 2



Introduction:

Apache Struts 2 is an elegant and extensible framework for building enterprise-class Java Web applications. The structure was designed to streamline the entire development cycle from design through implementation to maintenance of the application.

Apache Struts2 was originally known as WebWork 2. After several years of independent work, the WebWork and Struts communities have joined forces to create Struts2. This new version of Struts is easier to use and closer to Struts' call.

Struts 2 is a pull MVC structure. That is, the data to be displayed to the user must be extracted from the action.

Struts2 supports settings based on annotations that are easy to create and more intuitive. The action class Struts 2 acts as a template in the web application. Unlike Struts 1.X, the Struts 2-Action class is a simple POJO object that simplifies the code test. Struts2 also includes a configuration API to configure the interceptors that significantly reduce the coupling in the application. The visualization portion of Struts 2 is highly configurable and supports several types of results such as Velocity, FreeMarker, JSP, etc

 

Architecture:

 


Above diagram describes modules in struts 2. There are 5 modules:
1. Client-Client generates request which is processed by webContainer. Example of client can be web browser.
2.  Interceptor-Interceptors can execute code before and after an Action is invoked. Most of the framework’s core functionality is implemented as Interceptors. Features like double-submit guards, type conversion, object population, validation, file upload, page preparation, and more  are all implemented with the help of Interceptors. Each and every Interceptor is pluggable, so you can decide exactly which features an Action needs to support.
3.  Strut2 xml– Struts2.xml acts as a router which invokes action class for client request.
4.   Action-Action module considered as a model class which invokes business service and function logic.
5.   JSP-It is view part. According to action, Corresponding JSP files is rendered and result is returned to user.

 

Request Processing Lifecycle




1.     User generates request which then goes to servlet container.
2. Servlet container invokes FilterDispatcher filter which in turn directs to appropriate action.
3.  One by one Intercetors are applied before calling the Action. Interceptors performs tasks such as Logging, Validation, File Upload, Double-submit guard etc.
4.     Action is executed and the Result is generated by Action.
5.    The output of Action is rendered in the view (JSP, Velocity, etc) and the result is returned to the user.

Comparing between struts 1.x and struts 2.x


1. Struts1 extends the abstract base class by its action class. The problem with struts1 is that it uses abstract classes and no interfaces. A Struts 2 action can implement an action interface along with other interfaces to enable optional and custom services. Struts 2 provides a basic ActionSupport class for implementing commonly used interfaces. Although the action interface is not necessary. Any POJO object with an execution signature can be used as an Action Struts 2 object.

2. Struts 1 Actions are singletons and must be thread-safe, as only one instance of a class handles all the Action.Struts 2 action objects created for each request, so there is no thread security issue.

3.Aspire 1 Actions have dependencies in the servlet API because HttpServletRequest is passed to HttpServletResponse and the procedure for execution when an action is called. Struts 2 The actions are not assigned to a container. In most cases, servlet contexts are represented as simple maps so that actions can be tested individually.

4. A major obstacle to the Struts 1 action test is that the runtime method exposes the servlet API. A third-party extension, Struts Testcase provides a set of false Struts 1. Struts 2 actions can be tested by instantiating the action, setting properties, and invoking methods. Dependency injection support also simplifies the test.

5. ActonForms are used in Struts 1. These classes are assigned to jsp forms. Action classes use the data in these ActonForms to populate the data transfer objects. But in Struts 2, Acton Forms are not used. The JSP file is a direct mapping for POJO and no DTO files need to be created. The number of classes decreases, which reduces maintenance.

6. Struts 1 uses the standard JSP mechanism to associate objects with the context of the page to access them. 2 The Struts uses the "ValueStack" technology for taglibs to access values ​​without clutching their vision for the type of object they are doing.

7. Struts 1 supports separate query processors (lifecycles) for each module, but all actions in the module must have the same lifecycle. The Struts 2 supports the creation of different life cycles, through action, through the batteries of the interceptor. Custom stacks can be created as needed and used with different actions.
8. The Struts 1 is integrated into the JSTL, so it uses the JSTL EL. The EL has a basic object-graphics trajectory, but a relatively weak collection and indexed property support. 2 Struts can use JSTL, but the framework also supports a more powerful and flexible expression language called Graph Navigation Language Object (OGNL).




No comments:

Post a Comment

From Java 8 to Java 11

Switching from Java 8 to Java 11 is more complicated than most updates. Here are some of my notes on the process. Modules Java 9 i...