At line 25 changed 1 line. |
Now, struts will not set a Date object on your form (even if you apply the datePatten dd/MM/yyyy). |
Now, struts will not set a Date object directly from your form (even if you apply the datePatten dd/MM/yyyy). |
At line 27 changed 1 line. |
So you need a field to capture the string input: |
So you need a field to capture the string input of the dob field, so now your Form looks like this: |
At line 30 changed 1 line. |
private String dobString; |
public MyForm { |
java.util.Date dob; |
String dobString; |
} |
At line 33 changed 1 line. |
Struts only seems to be able to set the date property as a java.lang.String... you will of course need getters and setters on these as well. |
You will of course need getters and setters on these as well. |
At line 54 changed 1 line. |
return this.datetimeOfOffence; |
return this.dob; |
At line 67 changed 6 lines. |
if (getDob() != null) { |
StringBuffer date = new StringBuffer(); |
dateTime.append(getDobString()); |
return dateTime.toString(); |
} |
return null; |
return this.dobString; |
At line 73 added 5 lines. |
public void setDobString(String dobString) { |
this.dobString = dobString; |
} |
|
|
At line 86 added 32 lines. |
Here are the DateConverter methods: |
|
{{{ |
|
private static SimpleDateFormat standardDateFormat = new SimpleDateFormat( |
"dd/MM/yyyy"); |
|
public static Date convertDateStringToDate(String dateStr) { |
try { |
return standardDateFormat.parse(dateStr); |
} catch (ParseException pe) { |
return null; |
} |
} |
|
public static String convertDateToDateString(Date date) { |
return standardDateFormat.format(date); |
} |
}}} |
|
|
Finally, you should validate your date string input, so in your struts validation.xml file you should |
have an entry that looks like: |
|
{{{ |
<form name="myForm"> |
<field property="dobString" depends="required,date" > |
<arg0 key='dob.message.key'/> |
<var><var-name>datePattern</var-name><var-value>dd/MM/yyyy</var-value></var> |
</field> |
</form> |
}}} |