At line 2 changed 1 line. |
uses a DOB where we want the user to enter a time in hours, minutes, and seconds: |
uses a DOB where we want the user to enter a date in days, months and years: |
At line 9 changed 1 line. |
to Strings in your form. |
between your POJO to your Form. |
At line 11 changed 2 lines. |
So in your form you have getDOB() which returns a string.... |
|
So in your form and your POJO, you have getDOB() which returns a date as well.... |
At line 22 changed 1 line. |
java.lang.String dob; - the bridge between the dob java.util.Date in your POJO and the form. |
java.util.Date dob; |
At line 26 changed 5 lines. |
The reason the dates are of type java.lang.String rather than java.util.Date in your |
form is that if you apply the struts datePattern dd/MM/yyy validator to the user input |
field, struts does not convert the string entered to a proper java.util.Date (that is |
my belief anyhow - please correct me if this is wrong).... struts only seems to be able |
to set the date property as a java.lang.String... |
Now, struts will not set a Date object directly from your form (even if you apply the datePatten dd/MM/yyyy). |
At line 27 added 11 lines. |
So you need a field to capture the string input of the dob field, so now your Form looks like this: |
|
{{{ |
public MyForm { |
java.util.Date dob; |
String dobString; |
} |
}}} |
|
You will of course need getters and setters on these as well. |
|
At line 40 removed 10 lines. |
So we, we need to create 4 fields on our form, to capture the user inputting the dobStr, hour, minute, and second. So now our form looks like this: |
|
{{{ |
public MyForm { |
private String dob; - the bridge between the dob java.util.Date in your POJO and the form. |
java.lang.String hour; |
java.lang.String minute; |
java.lang.String second; |
java.lang.String dobStr; |
} |
At line 51 changed 2 lines. |
set on your form (in your chosen dateFormat), and the hours, minutes and seconds are set |
from the drop downs. |
set on your form (in your chosen dateFormat). |
At line 56 removed 3 lines. |
public String getDob() { |
if (getDateOfOffenceString()!=null && getHourOfOffenceString()!=null && |
getMinuteOfOffenceString()!=null) { |
At line 60 changed 9 lines. |
StringBuffer dateTime = new StringBuffer(); |
dateTime.append(getDateOfOffenceString()); |
dateTime.append(" "); |
dateTime.append(getHourOfOffenceString()); |
dateTime.append(":"); |
dateTime.append(getMinuteOfOffenceString()); |
return dateTime.toString(); |
} |
return null; |
public Date getDob() { |
if (getDobString() != null) { |
this.dob = DateConverter.convertDateStringToDate(getDobString()); |
} |
|
return this.dob; |
At line 70 removed 1 line. |
}}} |
At line 72 changed 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) |
public void setDob(Date dob) { |
this.dob = dob; |
At line 76 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?) |
if (dob != null) { |
setDobString(DateConverter.convertDateToDateString( |
dob)); |
} |
} |
At line 69 added 13 lines. |
public String getDobString() { |
return this.dobString; |
} |
|
public void setDobString(String dobString) { |
this.dobString = dobString; |
} |
|
|
}}} |
|
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> |
}}} |