Your First Struts Application

Your First Struts Application
Here's a tutorial to help you start building serious Web apps.
by Budi Kurniawan

Posted September 13, 2002

The old adage, "Work smarter, not harder," is sometimes a lot easier to say than to practice. But once you master Struts—a framework for building serious Web applications—you can develop applications much more rapidly than you do now. Unfortunately, most programmers find the required learning curve steep. This article, the first of a six-part series, provides a quick start for those who've been working with servlets or JSP pages but are new to Struts.

I'll teach you how to install and configure Struts, and present the specification and deployment descriptor of a small application built within the framework. I'll discuss the application in more detail in the second part of this series.

An important note before you start: Building this application requires a good understanding of the Model-View-Controller (MVC) paradigm in a Java Web application. If this is a new concept to you, please read "Almost All Java Web Apps Need Model 2."

First, though, you need to understand a few basics about Struts. It uses the Model 2 architecture, which is based on the MVC design pattern. Model 2 applications are ideal for serious developers because they create programs that are flexible, extensible, and easy to maintain. Model 2 is the recommended architecture even for simple applications, so it's crucial to have a framework on which you can build this kind of application quickly and easily. Apache's Jakarta Struts Project from Apache Software Foundation is such a framework, and this example will help you understand how to use it.

This example is a beginner's teaching tool, so it doesn't use the Struts capability fully. For example, it doesn't use Struts tag libraries or other interesting features, so the code is easier to understand. Also, in the interest of simplicity, it uses only a few files. Finally, you'll see Java code in the JSP pages that would be implemented using Beans in real-world applications, but this would needlessly complicate the example.

Now, before you can start, you need Struts in your machine. If you have not done so, download and configure Struts.

Installing Struts
Installing Struts is easy, and Struts does not need complicated configuration. First, download the Struts binary distribution from http://jakarta.apache.org/site/binindex.html. At the time of writing, version 1.0 is the stable version, and 1.1 is in beta. (I used 1.1-b2 for the projects in this series.)

You can then extract the compressed binary distribution into a temporary directory. The installation directory has three files: README, INSTALL, and LICENSE. These are self-explanatory. Two directories will be created under the installation directory: lib and webapps. The lib directory contains the struts.jar file, the commons-*.jar files, and other files you need to deploy in every Struts application. The webapps directory contains eight .war files, which contain the documentation and some sample applications built using Struts.

Your First Struts Application (Continued)

Struts is written in Java, so it requires JDK 1.2 or later. It also needs an XML parser compatible with the Java API for XML parsing (JAXP) specification. You need to include the XML parser library (jaxp.jar/parser.jar/another file) to your CLASSPATH environment variable. Also, if you're working with databases, you need to download and install the JDBC 2.0 Optional Package Binary from http://java.sun.com/products/jdbc. (If you use JDK 1.4, the XML parser and JDBC 2.0 Optional Package Binary have been included by default.)

Also, Struts needs a servlet container that supports Servlet API 2.2 or later. For the list of servlet containers that have been confirmed to work with Struts, see the INSTALL file.

Finally, to make Struts work, add an environment variable JAVA_HOME that points at the base directory of your JDK install directory, and copy the .jar files to every application's WEB-INF/lib directory.

The Login Application
This example is a simple login application that uses only the view and the controller from the MVC model. It lets users type in a user name and password before they can view the company's secret account number. The first page displayed is the Login page (see Figure 1). If login is successful, the user sees the Main Menu page (see Figure 2), which has two links: one to log out, and one to see the company's secret account number. If login fails, the user is redirected back to the Login page.

Figure 1. Logging In

If a user enters the correct user name and password, a session object is created for that user, and an attribute called loggedIn is added to the session object. The Main Menu page and the Secret page can be viewed only if the application can find the loggedIn attribute in the user's session object. So the absence of this attribute or the unavailability of a session object will force the user to log in. (The correct user name is "john," and the correct password is "123." These values have been hard-coded to avoid having to connect to a database or use other storage.)

When the user logs out, his or her session object will be invalidated, and the Login page will display again. When the user clicks on the link to view the company's secret, the Secret page will be displayed (see Figure 3).

Your First Struts Application (Continued)

Three JSP pages serve as the view in the MVC paradigm: login.jsp (see Figure 1), mainMenu.jsp (see Figure 2), and viewSecret.jsp (see Figure 3).

Figure 2. The Main Event

Because Struts applications are Model 2 applications, they must have something—in this case, a controller servlet—that acts as the controller in the MVC paradigm. Using Struts, you don't write your own controller servlet. It is already built for you, which is just one of the ways that Struts can speed up application development.

The controller servlet in a Struts application is an instance of the org.apache.struts.action.ActionServlet class. This class is just one of the many classes bundled together in the struts.jar file, so you need to deploy this file with every Struts application you develop.

Figure 3. You've Got a Secret

The controller servlet dispatches requests to appropriate views based on the URIs of the requests. By convention, this URI ends with .do. For example, the request's URI to the Secret page is http://domain/appName/viewSecret.do, and the URI to the Login page is http://domain/appName/login.do. (There could be other information after the URI, of course.)

If you've built servlet or JSP applications, you've probably guessed that servlet mappings are used in the deployment descriptor, so the requests are passed to the ActionServlet instance. You're right; Struts applications are just other servlet/JSP applications. The deployment descriptor for the application is shown in Listing 1. This code demonstrates three important points:

  • In the action servlet configuration section, the instance of the org.apache.struts.action.ActionServlet class is called action.
  • All URLs ending with .do are mapped to the action servlet.
  • login.jsp is the welcome file, the default file that will be called if the URL does not contain a resource name (in other words, when the URL is of the form http://domain/myApp).

Also, there is nothing special about the deployment descriptor in Listing 1. It simply shows that Struts applications are like normal servlet applications.

Requests for resources in the application should go to the ActionServlet, which forwards control to one of the objects, called action objects. I'll discuss action objects and the rest of the application in the second part of this article series.

You now know how to install and configure Struts, and you understand the login application's project specification and deployment descriptor. Download the complete application here.

About the Author
Budi Kurniawan is an IT consultant specializing in Internet and object-oriented programming and has taught both Java and Microsoft technologies. He is the author of the best-selling Java for the Web with Servlets, JSP, and EJB: A Developer's Guide to Scalable Solutions (New Riders) and the developer of the most popular Java Upload Bean from BrainySoftware.com, which is licensed and used in projects in major corporations. Contact Budi at budi@brainysoftware.com.



The Deployment Descriptor (web.xml) File

Listing 1. There is nothing special about this deployment descriptor. It simply shows that Struts applications are like normal servlet applications.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

  <!-- Action Servlet Configuration -->

  <!-- Action Servlet Mapping -->

  <!-- The Welcome File List -->

想对作者说点什么? 我来说一句

Creating Your First LiveCycle Application

2011年05月18日 971KB 下载

Creating your First Web Dynpro Application

2008年11月10日 415KB 下载



Your First Struts Application