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


This is version 5. It is not the current version, and thus it cannot be edited.
[Back to current version]   [Restore this version]


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 on your form (even if you apply the datePatten dd/MM/yyyy).

So you need a field to capture the string input:

    private String dobString;

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.

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.datetimeOfOffence;
    }

    public void setDob(Date dob) {
        this.dob = dob;

        if (dob != null) {
            setDobString(DateConverter.convertDateToDateString(
                    dob));
        }
    }

    public String getDobString() {
        if (getDob() != null) {
            StringBuffer date = new StringBuffer();
            dateTime.append(getDobString());
            return dateTime.toString();
        }
        return null;
    }

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



Go to top   More info...   Attach file...
This particular version was published on 06-Nov-2006 13:52:44 MST by BenGill.