ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Learning Jakarta Struts, Part 2
Pages: 1, 2, 3

Develop any classes or APIs necessary to meet screen requirements

All form classes in Struts extend org.apache.struts.action.ActionForm. The form class is used to correspond to the data fields on a screen. The form class can also be used for validation on form fields. The Setter/Getter accessor methods are called automatically on an HTML form submit, thereby being available to the action class for future use. Usually, an ActionForm class handles all of the variables on a form, and has the necessary validation for a specific screen. I tend to keep all of my form and action classes in separate packages; otherwise, it can get too confusing once you get more than a couple of screens in place. The following is a sample ActionForm class. This is the form that is used for the login page.



/*
* LoginForm.java
*/
package com.oreilly.forms;
 
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
 
/**
* Form bean for the Login main page. There are two 
* fields on this form used for authentication
* <ul>
* <li>username - the username to login
* <li>password - the password to authenticate
* </ul>
*/
public final class LoginForm extends ActionForm {
private String userName = null;
private String password = null;

/**
* Get the userName
*@return String 
*/
public String getUserName() {
return (userName);
}
/**
* Set the userName.
* @param userName 
*/
public void setUserName(String newUserName) {
userName = newUserName;
}
/**
* Get the password
*@return String 
*/
public String getPassword() {
return (password);
}
/**
* Set the password.
* @param password 
*/
public void setPassword(String newPassword) {
password = newPassword;
}
 
/**
* Reset all properties to their default values.
*
* @param mapping The mapping used to select this instance
* @param request The servlet request we are processing
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
userName = null;
password = null;

}
/**
* Validate the properties that have been set from this 
* HTTP request, and return an 
* <code>ActionErrors</code> object that 
* encapsulates any validation errors that have been 
* found. If no errors are found, return
* <code>null</code> or an 
* <code>ActionErrors</code> object with no
* recorded error messages.
*
* @param mapping The mapping used to select this instance
* @param request The servlet request we are processing
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
// For this form, only a username is required
if( userName == null || userName.length()==0 ){
errors.add("userName",new ActionError("error.userName.required"));
}
return (errors);
}
}

There are a couple of interesting points to be made here. The reset method is called if there is a reset button on a form. It sets all of the variables back to their defaults. The validate method is used for any form-specific validation. In this case, we are only interested if the user has provided their username. It might be possible to have no password, so we will let the application logic handle that. Without a username, the user can't be authenticated, so we are required to throw an error.

Errors in Struts can be handled using ActionErrors. Typically, anything ending in "s" is a collection of those objects. So ActionErrors is a collection of ActionError. You can process an entire form and send back multiple errors to display. This is convenient on entry forms that have many required fields, so that the user doesn't have to submit the form five times to get five different errors.

Also note that the errors shown in this sample use the ApplicationResource.properties file. The properties file is defined in the web.xml file used by the entire application when Tomcat starts. Usually there is a web.xml file in the WEB-INF directory of each application. For more information on how application structures are set up for deployment, see the user's guide available with the Tomcat download.

The ApplicationResource file allows for all strings displayed in an application to be defined. A lookup is done with a key. In this sample, the error.userName.required key will be replaced with the text for that error message -- "A username is required." This is especially handy for providing multilingual support for your application. Only the ApplicationResource needs to be translated, and you can then have a Spanish, German, or French version of your application.

This can be used for anything that is text displayed on a screen. The ApplicationResource file associated with this project looks like this:

login.title=Login Struts Sample
error.userName.required=A username is required
error.login.authenticate=Invalid username/password
errors.footer=</ul><hr>
errors.header=<h3><font color="red">Page 
Validation</font></h3>Please correct the 
following error(s) before contiuing:<ul>
applicationResources=Cannot load application resources bundle 
{0}

The title of pages, buttons, and anything requiring text can be accessed from this file. We'll show how to use the Struts tags to access the file from a JSP when we get to that development step in the final article of this series.

Sue Spielman is an associate editor for ONJava.com, covering JSP and Servlets technologies. She is also President and Senior Consulting Engineer for Switchback Software LLC.


Read more JSP and Servlets columns.

Return to ONJava.com.