Thanks to Struts, input validation programming can be achieved in much shorter time. When the user submits a form, Struts encapsulates the request parameters in a JavaBean called an action form. Because an action form is a JavaBean, it is often called a Form Bean. This book consistently uses the term “action form”.
An action form class must extend the org.apache.struts.action.ActionForm class. An action form has zero or many properties. A readable property must have a get method and a writeable property must have a set method. Each property in an action form class corresponds to a field in the HTML form displayed in the user’s browser. An action form’s property and its associated field must have the same name.
Suppose you have the following HTML form in your browser:
<form action="/actionPath"> Product Name: <input type="text" name="productName"/> <br/> Description: <input type="text" name="description"/> <br/> <input type="submit"/> </form>
and you have defined the following action form class named ProductForm:
import org.apache.struts.action.ActionForm;
public final class ProductForm extends ActionForm {
private String productName;
private String description;
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
When the HTML form is submitted, Struts will create an instance of ProductForm and populates its properties. The productName property will have the value of whatever the user entered into the productName field; the value of the description property will be the same as the value the user typed in to the description field.
To enable Struts to create the correct action form, you register the logical name and the Java type of the action form using the form-bean element in the Struts configuration file, such as this:
<form-bean name="productForm" type="myPackage.ProductForm"/>
The name attribute is an identifier or logical name for that action form throughout the whole application. As such, this name must be unique. The type attribute contains the fully qualified name for a Java class that represents the action form. You can have as many form-bean elements as you want. All form-bean elements must be child elements of the form-beans element:
<struts-config>
<form-beans>
<form-bean name="productForm" type="myPackage.ProductForm"/>
<!-- other form-bean elements go here -->
</form-beans>
</struts/config>
To make Struts create an action form for a particular action, you use the name attribute of the action element representing that action. For example, if you want Struts to create a productForm every time the saveProduct action is invoked, you write the following action element in your struts-config.xml file:
<action path="saveProduct" type="anActionClass" name="productForm"/>
An instance of myPackage.ProductForm will then be created for that action and this instance will be passed to the execute method on the action object created for that action. Recall that the execute method of the org.apache.struts.action.Action class has the following signature:
public ActionForward execute(ActionMapping mapping, ActionForm form, javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) throws java.lang.Exception
In the action class, you can then downcast the ActionForm object to your action form type (because an action form always extends org.apache.struts.action.ActionForm) and access its properties. For example:
ProductForm productForm = (ProductForm) form; String productName = productForm.getProductName(); String description = productForm.getDescription();
Of course, you can get the values of productName and description by calling the getParameter method on the HttpServletRequest object, but in a moment you’ll see why it makes sense to use action forms.
You must take the following steps to use an action form:
Now let’s have a look at the org.apache.struts.action.ActionForm class.