At line 22 removed 83 lines. |
|
This is how you could implement date handling using Struts / AppFuse. The example |
uses a DOB where we want the user to enter a time in hours, minutes, and seconds: |
|
In the POJO, your date is likely to be a java.util.Date or java.sql.Timestamp... |
so you have a method called getDOB() which returns a java.util.Date (for this example). |
|
When, in your action, you convert your POJO to the form, the converters |
(statically initialised in the BaseAction), are setup to convert the Date(s) |
to Strings in your form. |
|
So in your form you have getDOB() which returns a string.... |
|
{{{ |
public MyPOJO { |
java.util.Date dob; |
} |
}}} |
|
and |
{{{ |
public MyForm { |
java.lang.String dob; - the bridge between the dob java.util.Date in your POJO and the form. |
} |
}}} |
|
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... |
|
Also note: the datePattern does not work if you try and apply dd/MM/yyyy HH:mm - |
this is not so much of a problem, as it is not particularly user friendly to make |
the user input this long string freetext anyhow... so it is best to allow the user |
to enter hours and minutes using drop downs (populated from the StartupListener)... |
|
(BTW - a datePattern of just HH:mm does not seem to work either, so freetext entry of hours |
and minutes is awkward unless someone writes a custom validator)... |
|
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; |
} |
So you have applied the datePattern to the user input field (dateStr), and a String is |
set on your form (in your chosen dateFormat), and the hours, minutes and seconds are set |
from the drop downs. |
|
We now need to do a bit of work in the getDob() method of the form: |
{{{ |
public String getDob() { |
if (getDateOfOffenceString()!=null && getHourOfOffenceString()!=null && |
getMinuteOfOffenceString()!=null) { |
|
StringBuffer dateTime = new StringBuffer(); |
dateTime.append(getDateOfOffenceString()); |
dateTime.append(" "); |
dateTime.append(getHourOfOffenceString()); |
dateTime.append(":"); |
dateTime.append(getMinuteOfOffenceString()); |
return dateTime.toString(); |
} |
return null; |
} |
}}} |
|
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) |
|
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?) |
|
Tip: If you are having problems with the Converter(s), put this in your log4j config: |
|
log4j.logger.org.apache.commons.beanutils=DEBUG |
|