Raible's Wiki
Raible Designs AppFuseHomepage- Korean - Chinese - Italian - Japanese QuickStart Guide User Guide Tutorials Other ApplicationsStruts ResumeSecurity Example Struts Menu
Set your name in
UserPreferences
Referenced by
JSPWiki v2.2.33
Hide Menu |
This is version 9.
It is not the current version, and thus it cannot be edited. This is how you could implement date handling using Struts / AppFuse. The example uses a DOB where we want the user to enter a date in days, months and years: 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) between your POJO to your Form. So in your form and your POJO, you have getDOB() which returns a date as well.... public MyPOJO { java.util.Date dob; } and public MyForm { java.util.Date dob; } Now, struts will not set a Date object directly from your form (even if you apply the datePatten dd/MM/yyyy). 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. 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 you have applied the datePattern to the user input field (dateStr), and a String is set on your form (in your chosen dateFormat). We now need to do a bit of work in the getDob() method of the form: public Date getDob() { if (getDobString() != null) { this.dob = DateConverter.convertDateStringToDate(getDobString()); } return this.dob; } public void setDob(Date dob) { this.dob = dob; if (dob != null) { setDobString(DateConverter.convertDateToDateString( dob)); } } 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) Tip: If you are having problems with the Converter(s), put this in your log4j config: log4j.logger.org.apache.commons.beanutils=DEBUG 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); }
|