Tech News

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.

The growth in Struts’ popularity began to lose momentum because of the ever-increasing complexities of web applications and because of the competition from other evolving web frameworks. The WebWork framework that was built on classic Struts later unified with it to create the Struts 2 framework. Struts 2 is a complete rewrite of the architecture of classic Struts, aimed at addressing the aforementioned needs. Struts 2 provides the architectural foundations for web applications, provides architectural mechanisms to automate recurring tasks and to separate cross-cutting concerns, and saves developers from maintaining a plethora of configuration code by means of convention over configuration.

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.

Even though you can use POJO actions, Struts 2 provides two action helpers that can be used: the Action interface and the ActionSupport

ActionSupport

The ActionSupport implements the Action interface and provides an implementation of the execute() method that returns the SUCCESS value. The ActionSupport class also implements some interfaces that provide support for validation, localization, and internationalization, as illustrated in the code fragment of the ActionSupport.

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.

ValueStack 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. As you saw in Figure 4-2, OGNL and the ValueStack, though not part of MVC, is at the core of the Struts 2 framework. All the MVC components interact with ValueStack to provide contextual data.

These components access ValueStack using OGNL syntax, and OGNL and ValueStack work together in Struts 2 to handle a request. Specifically, when a request is sent to the Struts 2 application, a ValueStack 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 ValueStack. 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 $.

ResultType and Result

In 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.

Summary

Struts 2 framework’s core components, 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.

The Women Delusion Calculator is constantly evolving with advanced features. Future developments will focus on enhancing user experience. Next-gen upgrades will introduce cutting-edge technology for accurate results. The calculator is expanding its scope to include other demographics, catering to a wider audience.

Related Articles

Leave a Reply

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

Back to top button