Chapter 1: Introducing the Jakarta Struts Project and its Supporting Components

 < Day Day Up > 



In this chapter, we lay the foundation for all of our further discussions. We start by providing a high-level description of the Jakarta Struts project. We then describe Java Web applications, which act as the packaging mechanism for all Struts applications. We conclude this chapter with a discussion of the Jakarta Tomcat JSP/servlet container, which we use to host all of our examples throughout the remainder of this text.

At the end of this chapter, you should have an understanding of what the Struts project is, be familiar with its packaging mechanism, and have an installed JSP/servlet container to run your Struts applications.

The Jakarta Struts Project

The Jakarta Struts project, an open-source project sponsored by the Apache Software Foundation, is a server-side Java implementation of the Model-View-Controller (MVC) design pattern. The Struts project was originally created by Craig McClanahan in May 2000, but since that time it has been taken over by the open-source community.

The Struts project was designed with the intention of providing an open-source framework for creating Web applications that easily separate the presentation layer and allow it to be abstracted from the transaction and data layers. Since its inception, Struts has received quite a bit of developer support and is quickly becoming a dominant factor in the open-source community. This book covers version 1.1 of the Jakarta Struts Project.

start sidebar

A small debate is going on in the development community as to the type of design pattern that the Struts project most closely resembles. According the documentation provided by the actual developers of the Struts project, it is patterned after the MVC, but some folks insist that it more closely resembles the Front Controller design pattern described by Sun's J2EE Blueprints Program. The truth is that it does very much resemble the Front Controller pattern, but for the purpose of our discussions, I am sticking with the developers. If you would like to examine the Front Controller yourself, you can find a good article on this topic at the Java Developer Connection site, http://developer.java.sun.com/developer/technicalArticles/J2EE/despat/.

end sidebar

Understanding the MVC Design Pattern

To gain a solid understanding of the Struts Framework, you must have a fundamental understanding of the MVC design pattern, which it is based on. The MVC design pattern, which originated from Smalltalk, consists of three components: a Model, a View, and a Controller. Table 1.1 defines each of these components.

Table 1.1: The Three Components of the MVC

Component

Description

Model

Represents the data objects. The Model is what is being manipulated and presented to the user.

View

Serves as the screen representation of the Model. It is the object that presents the current state of the data objects.

Controller

Defines the way the user interface reacts to the user's input. The Controller component is the object that manipulates the Model, or data object.

We discuss each of these components in more detail throughout this chapter. Some of the major benefits of using the MVC are:

  • Reliability: The presentation and transaction layers have clear separation, which allows you to change the look and feel of an application without recompiling Model or Controller code.

  • High reuse and adaptability: The MVC lets you use multiple types of views, all accessing the same server-side code. This includes anything from Web browsers (HTTP) to wireless browsers (WAP).

  • Very low development and lifecycle costs: The MVC makes it possible to have lower-level programmers develop and maintain the user interfaces.

  • Rapid deployment: Development time can be significantly reduced, because Controller programmers (Java developers) focus solely on transactions, and View programmers (HTML and JSP developers) focus solely on presentation.

  • Maintainability: The separation of presentation and business logic also makes it easier to maintain and modify a Struts-based Web application.

The Struts Implementation of the MVC

The Struts Framework models its server-side implementation of the MVC using a combination of JSPs, custom JSP tags, and a Java servlet. In this section, we briefly describe how the Struts Framework maps to each component of the MVC. When we have completed this discussion, we will have drawn a portrait similar to Figure 1.1.

click to expand
Figure 1.1: The Struts implementation of the MVC.

Figure 1.1 depicts the route that most Struts application requests follow. This process can be broken down into five basic steps. Following these steps is a description of the components involved in this sequence.

  1. A request is made from a previously displayed View.

  2. The request is received by the ActionServlet, which acts as the Controller, and the ActionServlet looks up the requested URI in an XML file (described in Chapter 3, "Getting Started with Struts"), and determines the name of the Action class that will perform the necessary business logic.

  3. The Action class performs its logic on the Model components associated with the application.

  4. Once the Action has completed its processing, it returns control to the ActionServlet. As part of the return, the Action class provides a key that indicates the results of its processing. The ActionServlet uses this key to determine where the results should be forwarded for presentation.

  5. The request is complete when the ActionServlet responds by forwarding the request to the View that was linked to the returned key, and this View presents the results of the Action.

start sidebar

As you can probably surmise, the previous steps are a simplified representation of a Struts request. We cover these steps in much more detail, including a discussion on several other components, as this text progresses.

end sidebar

The Model

The Model components of the Struts Framework, as we stated earlier, represent the data objects of the Struts application. They often represent business objects or other backend systems and can be implemented as simple JavaBeans, Enterprise JavaBeans, object representations of data stored in a relational database, or just about anything that needs to be manipulated or presented using a Web application. We take a look at the Model component in greater detail in Chapter 11, "Integrating the Jakarta Commons Database Connection Pool (DBCP)."

The View

Each View component in the Struts Framework is mapped to a JSP that can contain any combination of HTML, JSP, and Struts custom tags. JSPs in the Struts Framework serve two main functions. The first is to act as the presentation layer of a previously executed Controller Action. This is most often accomplished using a set of custom tags that are focused around iterating and retrieving data forwarded to the target JSP by the Controller Action. This type of View is not Struts specific and does not warrant special attention.

The second of these functions, which is very much Struts specific, is to gather data that is required to perform a particular Controller Action. This is accomplished most often with a combination of Struts tag libraries and ActionForm objects. This type of View contains several Struts-specific tags and classes. The following code snippet contains a simple example of this type of Struts View:

 <%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"> <html:form action="loginAction.do"   name="loginForm"   type="com.wrox.loginForm" >   User Id: <html:text property="username"><br/>   Password: <html:password property="password"><br/>   <html:submit /> </html:form> 

As you can see, several JSP custom tags are being leveraged in this JSP. These tags are defined by the Struts Framework and provide a loose coupling to the Controller of a Struts application. We build a working Struts View in Chapter 3, and in Chapter 6, "Building the Presentation Layer," we examine the Struts Views in more detail.

start sidebar

While JSPs are the common presentation tool in a Struts application, they are not the only presentation method available. You can leverage many other tools, including template tools, the Jakarta Velocity Project, and XSLT, among others. For the purpose of this text, we focus on the default Struts presentation layer: JSPs.

end sidebar

The Controller

The Controller component of the Struts Framework is the backbone of all Struts Web applications. It is implemented using a servlet named org.apache.struts.action.ActionServlet. This servlet receives HTTP requests and delegates control of each request, based on the URI of the incoming request, to a user-defined org.apache.struts.action.Action class. The Action class is where the Model of the application is retrieved and/or modified. Once the Action class has completed its processing, it returns a key to the ActionServlet. This key is used to determine the View that will present the results of the Action class's processing. You can think of the ActionServlet as a factory that takes named requests for services and based upon these requests creates Action objects to perform the actual business logic required to complete these services.

The Controller is the most important component of the Struts Framework. We discuss the Controller in Chapter 3, "Getting Started with Struts," and in even greater detail in Chapter 4, "Actions and the ActionServlet."



 < Day Day Up > 



Professional Jakarta Struts
Professional Jakarta Struts (Programmer to Programmer)
ISBN: 0764544373
EAN: 2147483647
Year: 2003
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net