3.13. Handling Date Input Fields
Problem
You want to allow a user to input a value for a calendar date.
Solution
First, only use
String form fields to
hold the input date values. If the user can manually type in the
date, it helps if you provide graphical Calendar control, either
client- or server-based, that can populate the date input fields
automatically. When the date value is submitted, validate the input
using the Struts Validator.
Here's a
DynaActionForm, JSP
page, and Action that demonstrate this approach.
The DynaActionForm is declared with the following
form-bean element:
<form-bean name="DateForm"
type="org.apache.struts.validator.DynaValidatorForm">
<!-- Date 1 -->
<form-property name="month" type="java.lang.String"/>
<form-property name="day" type="java.lang.String"/>
<form-property name="year" type="java.lang.String"/>
<!-- Birth Date-->
<form-property name="birthDateString" type="java.lang.String"/>
<!-- Date 3 -->
<form-property name="monthOpt" type="java.lang.String"/>
<form-property name="dayOpt" type="java.lang.String"/>
<form-property name="yearOpt" type="java.lang.String"/>
</form-bean>The JSP page shown in Example 3-19 (date_test.jsp) renders
fields for inputting data for these three variations of date formats.
The first variation uses three numeric fields to accept the month,
day, and year. The second variation accepts the date as a single
value in mm/dd/yyyy format. This variation utilizes a JavaScript calendar that can be used to pick the value. The third variation ...