The previous example did not show the full capability of the action form. In truth, the action form offers two more features with regard to input validation:
To enjoy these features, you must use the tags in the HTML Tag Library, one of the three core tag libraries that come with Struts. The JAR file and the tag descriptor (struts-html.tld file) for the HTML Tag Library are included in the struts.jar file. However, to indirectly reference the TLD file, you must copy the struts-html.tld file to the WEB-INF directory.
To see these features in action, let’s now modify the app02a application into app02b. The only files you need to change are the web.xml file and the displayAddProductForm.jsp, which are given in Listings 2.6 and 2.7, respectively.
Listing 2.6: Using <taglib> in the web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
</web-app>
Listing 2.7: Using the HTML Tag Library Tags in displayAddProductForm.jsp page
<%@ taglib uri="/tags/struts-html" prefix="html" %> <html> <head> <title>Add Product Form</title> </head> <body> <html:errors/> <h3>Add a product</h3> <html:form action="/saveProduct"> <table> <tr> <td>Product Name:</td> <td><html:text property="productName"/></td> </tr> <tr> <td>Description:</td> <td><html:text property="description"/></td> </tr> <tr> <td>Price:</td> <td><html:text property="price"/></td> </tr> <tr> <td><html:reset/></td> <td><html:submit>Add Product</html:submit></td> </tr> </table> </html:form> </body> </html>
In the displayAddProductForm.jsp page, note the use of <html:errors>, <html:form>, and <html:text>. <html:errors> is responsible for displaying error messages, and will be discussed in detail in Chapter 11, “Message Handling and Internationalization”.
The form tag represents an HTML form and its action attribute defines the action that will be invoked upon submission. In the displayAddProductForm.jsp page in Listing 2.7, the value of the action attribute is /saveProduct, which indicates the saveProduct action will be invoked upon the form submission.
The text tag represents an HTML field. Its property attribute specifies a property of the associated action form.
Note
The HTML Tag Library is discussed in Chapter 3.
Now, run the application by directing your browser to this URL:
http://localhost:8080/app02b/displayAddProductForm.do
You will see the same form as in Figure 2.2. Now, type in 50 to the Price field but leave the other two fields empty, then submit the form. Surprise, surprise, you will see two error messages and the previous value in the Price field, as depicted in Figure 2.5.
Figure 2.5: The error handling feature of the action form
The two features are explained briefly in the next sections.
Recall that Struts stores the instance of ActionForm it creates as an attribute of the HttpServletRequest or the HttpSession object, depending on the value of the scope attribute of the corresponding action element. The name used in the form-bean element is used as the attribute key. Therefore, if you define the following:
<form-bean name="productForm" type="myPackage.ProductForm"/>
the key used for the ProductForm instance will be productForm. If validation is not successful, Struts forwards control to the resource indicated by the input attribute. The tags in the HTML Tag Library can be used to access the action form in the HttpServletRequest or HttpSession object and redisplay its values. This library is discussed in detail in Chapter 3 but some of the tags are explained here.
The first tag is the form tag, which is translated into an HTML form. This tag has the action attribute that takes the path to a Struts action. When the form is submitted, the action whose path is in the action attribute will be invoked.
The second tag is <html:text property=”propertyName”>, which translates into <input type=”text” name=”propertyName”/>.
The text tag also accesses the action form associated with the enclosing form tag and copies the value of the action form’s associated property to the HTML field it represents. Therefore, if you write the following:
<html:text property="description">
the text tag will copy the value of the description property of the containing action form. There will be an exception thrown if the action form does not contain the specified property.
With this capability, the text tag can display the previous value the user typed in if validation fails. This is very convenient because that means the user does not need to retype the same value.
Recall that the validate method returns an ActionErrors object. If the ActionErrors object is not null and is not empty, Struts will assign it as an attribute in the HttpServletRequest object. As a result, the ActionErrors object is accessible from a JSP. The errors tag can be used to display the elements of the ActionErrors object. It iterates all the ActionError objects from the ActionErrors collection and matches the key of each ActionError object against a properties file.
How does Struts find this properties file. You tell it by using the message-resources element in the struts-config.xml file. For example, if you have the following message-resources element in your Struts configuration file:
<message-resources parameter="ApplicationResources"/>
Struts will take it that you have an ApplicationResources.properties file under the WEB-INF/classes directory.
If you have written these key/value pairs in your properties file:
key 1=error message 1 key 2=error message 2 ... key n=error message n
The errors tag will look up the keys in the properties file and displays the error messages for you.
Error handling will be discussed in more detail in Chapter 11.