Your First Struts Application

原创 2004年08月25日 17:13:00

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"
  "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
  <!-- Action Servlet Configuration -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  </servlet>

  <!-- Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <!-- The Welcome File List -->
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

Head First Servlet&Jsp 学习笔记(一) 一些基础知识

Head First Servlet&Jsp 学习笔记(一) 一些基础知识什么是Servlet和JSP用Java开发Web应用程序时用到的技术主要有两种,即Servlet和JSP。 Servlet是在...
  • Peng154
  • Peng154
  • 2015年11月06日 00:16
  • 1146

【经验】Android Gradle配置问题

翻墙后搞了一天Gradle,到处找资料,结果都不行,记录一点经验,以备之后再次出错 有时,配置Gradle出现如下错误Gradle DSL method not found:'android()' ...
  • chenjj2048
  • chenjj2048
  • 2016年04月03日 21:18
  • 310

使用Django搭建一个简单的Python Web工程

配置好python及django之后就可以使用框架搭建一个简单的web project
  • ps_zhanglei
  • ps_zhanglei
  • 2014年11月12日 16:19
  • 2360

20130218-初学STM32遇到的一些问题

1.设置工程时,如果忘记了在C/C++选项卡上设置STM32F10X_HD,USE_STDPEFIPH_DRIVER编译时会报如下的错误 Build target 'USART' compilin...
  • sy_lixiang
  • sy_lixiang
  • 2013年02月18日 10:24
  • 9675

spark-streaming入门(三)

Spark Streaming + Flume Integration Guide Apache Flume is a distributed, reliable, and available ser...
  • qq_23660243
  • qq_23660243
  • 2016年05月28日 14:42
  • 584

android 环境搭建

第一种比较简单: 首先下载和安装java环境, JDK,1.6与1.7皆可。我下载安装的是jdk1.7 http://www.oracle.com/technetwork/java/javase/...
  • nnjoshua
  • nnjoshua
  • 2013年04月27日 09:31
  • 1122

SparkStreaming整合Flume(一)Push方式的整合

SparkStreaming整合Flume(一)Push方式的整合
  • Wing_93
  • Wing_93
  • 2017年11月09日 19:43
  • 89

Spark源码解读之SparkContext初始化

SparkContext初始化是Driver应用程序提交执行的前提,这里以local模式来了解SparkContext的初始化过程。 本文以 val conf = new SparkConf()....
  • kangkangwanwan
  • kangkangwanwan
  • 2018年01月04日 19:01
  • 222

idea使用总结

1、Intellij IDEA启动时怎么选择项目? 按下【Ctrl+Alt+S】,在设置界面选择【System Settings】,把右边面版中Reopen last project on start...
  • gebitan505
  • gebitan505
  • 2016年06月30日 18:09
  • 2456

Excel的导入与导出

由于公司的很多系统都是好多年前做的,在公司的Office升级完成以后,公司内部的系统对新版本的Excel支持问题就一一浮现出来。越来越多的人在抱怨公司内部对excel2007和2010不支持的问题。最...
  • oatye40020
  • oatye40020
  • 2014年08月17日 16:01
  • 720
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Your First Struts Application
举报原因:
原因补充:

(最多只允许输入30个字)