Technology

Building a Web Application Using Struts 2

The Struts framework is an old living tree whose ring patterns tell the story of the legacy Java web forests. Struts, released in June 2001, pioneered an essential evolution of the Model-2 development model to address the vicissitudes of web development. You can see Struts’ DNA assimilated in many other architecturally diverse action-based frameworks that evolved to address Model-2 development. Struts gave birth to the Tiles framework, which can now be used with a myriad of web frameworks.

Struts 2 Framework Overview

Before diving into Struts 2, it is worthwhile to understand the architecture of classic Struts (hereafter referred to as Struts). Struts is an MVC-based framework. The central component of a Struts framework is the Action Servlet, which implements the Front Controller web-tier Java EE pattern. Illustrates the architecture of Struts.

Action

Actions are the core of the action-oriented Struts 2 framework because they provide the necessary logic for request processing. Actions are not required to implement any interface or extend any class, and actions can be POJOs. Illustrates the action called Hello World Action.

Interceptors

Interceptors promote the separation of concerns by separating the implementation of the cross-cutting concerns from the action. Struts 2 comes with a set of prebuilt interceptors and interceptor stacks that you can use out of the box. Illustrates the declaration of an action that belongs to a package that extends the struts-default package, which contains the default set of interceptors.

When you extend your package from the struts-default package, by default the default Stack will be used for all the actions in your package. The default Stack is configured in the struts-default.xml file, and it provides all the core Struts 2 functionality. The struts-default.xml file is located in the struts 2-core.jar file. To map other interceptors to action, you can use the interceptor-ref element,

Value Stack and OGNL

The Object-Graph Navigation Language (OGNL1) is a powerful expression language that is used to set and get properties from JavaBeans and to invoke methods from Java classes. It also helps in data transfer and type conversion. OGNL is similar to EL and JSTL, which evaluate expressions and navigate object graphs using dot notation. OGNL and the Value Stack, though not part of MVC, are at the core of the Struts 2 framework. All the MVC components interact with Value Stack to provide contextual data. These components access Value Stack using OGNL syntax, and OGNL and Value Stack work together in Struts 2 to handle a request. Specifically, when a request is sent to the Struts 2 application, a Value Stack object is created for that request, and the references to all the objects that are created to serve that request as well as scope attributes are maintained in the Value Stack. All these objects are available to view through OGNL. You will not find OGNL difficult to use because it is similar to EL and JSTL. Illustrates how OGNL looks. Notice that OGNL uses #, unlike JSP EL, which uses

Result Type and Result

Struts 2, the rendering of the response consists of the result type and the result. The result type provides the implementation details for the type of view that is returned to the user. Each method on an action returns a result, which includes specifying the result type. If no result type is specified, then the default result type is used, which forwards to a JSP page, as illustrated

Summary

You saw the Struts 2 framework’s core components, which follow the MVC design pattern, and you saw how struts 2separates the cross-cutting concerns by means of interceptors. You saw how the Struts 2 framework provides a declarative architecture in two forms: XML and annotations. In the next chapter, you will learn another action-oriented framework, called Spring Web MVC

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button