Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences


Referenced by
AppFuseSupport




JSPWiki v2.2.33

[RSS]


Hide Menu

AppFuseStrutsDates


Difference between version 10 and version 3:

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 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 three fields 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 3 lines.
private String dobString;
private String hourDobString;
private String minuteDobString;
public MyForm {
java.util.Date dob;
String dobString;
}
At line 35 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.
At line 45 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 56 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 61 removed 3 lines.
public String getDob() {
if (getDateOfOffenceString()!=null && getHourOfOffenceString()!=null &&
getMinuteOfOffenceString()!=null) {
At line 65 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 75 removed 1 line.
}}}
At line 77 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 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?)
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>
}}}

Back to AppFuseStrutsDates, or to the Page History.