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

Edit this page


Referenced by
AppFuseSupport




JSPWiki v2.2.33

[RSS]


Hide Menu

AppFuseStrutsDates


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);
    }

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>


Go to top   Edit this page   More info...   Attach file...
This page last changed on 06-Nov-2006 13:52:59 MST by BenGill.