Building Web Applications Using Struts

Introduction

 
Struts is an MVC based open-source framework for developing web applications. It uses standard technologies such as JavaBeans, Servlet, and JSP. It is designed and used with Java Platform Enterprise Edition (J2EE) platform, It reduces the development time and provides developers with a series of tools and components that help in building applications. It works with any J2EE compliant application server. Struts is a part of the Jakarta subproject of the Apache Foundation and have an Open Source License.
 
Struts also provide the framework for building web applications. It does so by providing libraries and utilities for an easier and faster web application development.
 

Struts Implementation of MVC Architecture


Struts implements MVC by providing a controller servlet that is used to manage flow within the JSP pages. The struts framework implements MVC by modeling its server-side using a combination of JSPs, custom tags, and servlets. Figure 1 shows the route followed by most struts applications.
 
A request arrives from View 1, which is then passed to the ActionServlet class, which acts as a controller. The ActionServlet class looks up the requested URI in an XML file and determines the name of the Actionclass that is needed to process the business logic. The Actionclass then performs its logic on the Model components that are associated with the application. After completion of the processing, the Action class returns the control back to the ActionServletclass.
 
The Actionclass also provides a key indicating the result of processing. This key is used by the ActionServletclass to determine the location for the results to be presented. The request is completed when theActionServlet class responds by forwarding the request to view 2 that was associated with the key and this View 2 presents the results of the action.
 
Architecting Web Applications Using Struts
 
Struts Model
 
The application's data as well as the logic required to interact with the data is characterized by the model. Strut gives the flexibility to the application to use any approach or technology for building the Model layer code. The Model component provides the capability of interacting with the standard data access technology such as EJB, Java Data Objects (JDO), and so on…
 
Struts View
 
The view components of struts are built using JSP pages. Some of the other components that can be used are HTML, JSP custom tag libraries, JavaScript and Stylesheets, Multimedia files, Message resource bundles, and ActionForm classes.
 
Struts Controller
 
Struts provides its own controller component in the form of a controller. Servlet and set of supporting classes. This controller Servlet is responsible for initializing the struts applications configuration file and receiving all incoming requests to the applications. There are Form Beans, which holds data for each HTML form. RequestProcessor class processes the request, which includes selecting, populating and validating the Form Bean and then selecting an appropriate Action class to actually execute the request. After the processing is complete, the Action class returns a key to the ActionServlet and this key determines the view, where the servlets will be presented.
 
Fig 2 shows the sequence of actions for an addBook scenario for a book application and the controller classes involved.
 
Architecting Web Applications Using Struts
 
The ActionServlet class is responsible for creating and populating the BookFormBean with data. It then passes it to the AddBookAction class object, which knows the specific action to be taken. The ActionServlet class consults the struts configuration file for the FormBean definition.
 

Alternatives to Struts Framework

 
Although Struts is a robust framework, there are alternative frameworks, environments for dynamic web application development.
 
Spring MVC
 
The framework has as the central element of a DispatcherServlet . This Servlet dispatches requests to various handlers. The default handler is a Controller interface that offers a ModelAndViewhandleRequest() method. The framework has an included implementation hierarchy consisting of AbstractController, AbstractCommandController and SimpleFormController. Application controllers will typically be subclasses of those.
Shale Framework
 
The major features of Shale Framework are:
  • View Controller
    This framework performs for associating a java class with each of JSF View in an application.
  • Dialog Manager
    The framework also supports a mechanism to represents a conversation with a user. This requires multiple HTTP requests to implement.
  • Application Manager
    This framework also provides front controller properties that should get applied to every request.
  • Validation
    This framework also provides integration with the JCVF (Jakarta Commons Validator Framework). Based on a single set of configured validation rules the framework supports client/server-side validations.
  • Integration
    This framework supports integration with the spring framework.
  • Clay
    It is also an alternative approach to JSP where we define views in pure HTML.
The framework supports Test Framework which is set of mock objects and Junit test case base classes suitable for testing both the framework classes as well as application components built on top of the framework, The framework also supports an optional add-on library named Tiger Extensions that adds additionally easy to use features for Shale applications that run on Java Standard Edition.
 

The Struts Component Packages

 
The Struts framework supports various packages, which a developer can use to build the components for the web application.
 
org.apache.struts.action
 
This package provides support to build the controller components. It contains the controller classes such as ActionForm, ActionMessages, and several other required framework components.
 
org.apache.struts.actions
 
This package also provides special adapters between the incoming HTTP Request & the corresponding Business logic. It contains action classes such as DispatchAction, that can be used or extended by applications.
 
org.apache.struts.config
 
The config package contains configuration objects that correspond to elements that may be specified in the struts-config.xml configuration file. These configuration classes are in-memory representations of the struts configuration file.
 
org.apache.struts.taglib
 
These taglib package contains the tag handler classes for the Struts tags libraries.
 
org.apache.struts.tiles
 
The tiles framework provides the facility of building web pages simply by assembling reusable pieces of pages, often called tiles.
 
org.apache.struts.upload
 
The upload package provides facilities to upload files using multi-part requests. It includes classes used for uploading and downloading files from the local file system, using a browser.
 
org.apache.struts.util
 
The util package provides a variety of families of classes, to solve problems that are commonly encountered in building web applications.
 
org.apache.struts.validator
 
This package performs a series of classes to validate ActionForm type of input.
 

Summary

 
The roadmap for web application development started with static web pages developments, which were connected using hyperlinks. To create dynamic web content Common Gateway Interface (CGI) scripts were used initially for server-side programming. With the introduction of Servlet, Java became the technology of choice for the server as well as client-side programming. JSP was introduced as the extension of Servlet technology. JSP pages combinedJava code and HTML code to create a web application. Two modules for creating JSP based application, the second module ie module2formed the basis of Struts Framework. This framework typically is a reusable, semi-complete application that can be customized for a specific application domain.


Similar Articles