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 75 changed 1 line. |
}}} |
public void setDobString(String dobString) { |
this.dobString = dobString; |
} |
At line 77 removed 3 lines. |
Now, when you convert the form back to a POJO, the DateConverter (utils), will |
convert the string back to a Date object (you need to check the DateConverter is applying the |
correct datePattern in the SimpleDateFormat to map to your datePattern) |
At line 81 changed 2 lines. |
Q) What happens if you have 2 dates in your form, and they have different datePatterns? |
A) I guess you would have to dynamically assign the correct converter (unregister DateConverter?) |
}}} |
At line 80 added 2 lines. |
Now, when you convert the form back to a POJO, the Converters will call getDob() which will convert the dob string into a Date and return that (so it is working in the same way that getAddressForm() getAddress() works inside the UserForm... doing a type conversion... (you need to check the DateConverter is applying the correct datePattern in the SimpleDateFormat to map to your datePattern) |
|
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> |
}}} |