Your First Struts Application

xiaoxiao2021-03-06  103

Your First Struts ApplicationHERE'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 does not use the Struts capability fully. For example, it does not 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 StrutsInstalling 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 ApplicationThis 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 2), And Viewsecret.jsp (See Figure 3).

Figure 2. The main evenet

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 do not write your own controller servlet. It is already built for you Which is Just One of the Ways That Struts CAN Speed ​​Up Application Developments.

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-sellingJava for the Web with Servlets, JSP, and EJB: A Developer's Guide to Scalable Solutions ( New riders) and the developer of the MOPULAR 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.

Public "- // Sun microsystems, Inc.//dtd Web Application 2.3 // en"

"http://java.sun.com/dtd/web-app_2_3.dtd">

Action

org.apache.struts.Action.ActionServlet

Action

*. do

Login.jsp