Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Subscription to Java Pro

email article
printer friendly
get the code

Grace Under Errors
Learning how to handle errors in Struts can make a huge difference in the quality of your programs.
by Budi Kurniawan

Posted October 28, 2002

Read this tutorial's first five installments:
Part 1: "Your First Struts Application"
Part 2: "Manage Control Flow in Struts Apps"
Part 3: "Move Onward and Upward With ActionForward"
Part 4: "Build Classier Struts Apps With ActionMapping"
Part 5: "Strut an ActionForm for All to See"

To err is human; to really mess things up requires an end user. That's why the ActionError and ActionErrors classes in the org.apache.struts.action package are such important classes; they do the heavy lifting of error handling in Struts applications. In Part 6 of this Struts tutorial series, you'll learn how to use them in a sample application, a modified version of the login application written for Part 5 of this series. Download the application (myStrutsApp5) here.

To understand error handling in Struts, you first need to learn the ActionError and ActionErrors classes. An ActionError instance can represent an error during user input validation or any other processing in a Struts application. And because a unit of processing can produce more than one error, ActionErrors stores a collection of ActionError objects. For example, in Part 5 you saw that the org.apache.struts.action.ActionForm has a validate method that returns an ActionErrors object. If you handle errors correctly, this ActionErrors object will contain all errors that occurred during that input validation within the ActionForm instance.

The ActionError class, which extends org.apache.struts.action.ActionMessage, is a simple class that encapsulates two units of data: a key and an array of error messages. You can use the key to look up message text in a message resource database, if you're using such a database. (You can also store message text in the array of values.) Therefore, an ActionError instance contains a key/value pair. The key is a string object, and the value is an array of replacement values. The ActionError class lets you store up to four replacement values. Normally, each replacement value contains an error message.

The ActionError class has six constructors. The first lets you create an ActionError object that has a key but no value. This kind of ActionError object will have its message text stored in a database, a text file, or any other resource. The constructor has this signature:

public ActionError(java.lang.String key)

The second, third, fourth, and fifth constructors let you define one, two, three, or four error messages. They take this form, where valuen indicates the (n+1)th replacement value (in other words, value0 indicates the first replacement value, and so on):

public ActionError(java.lang.String key,
                   java.lang.Object value0)

public ActionError(java.lang.String key,
  java.lang.Object value0,
  java.lang.Object value1)

public ActionError(java.lang.String key,
  java.lang.Object value0,
  java.lang.Object value1,
  java.lang.Object value2)

public ActionError(java.lang.String key,
  java.lang.Object value0,
  java.lang.Object value1,
  java.lang.Object value2,
  java.lang.Object value3)


Back to top













Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home