Results 1 to 8 of 8

Thread: Two CustomDateEditor: one for Date and other for Time?

  1. #1
    Join Date
    Oct 2004
    Location
    Palmas-TO, Brazil
    Posts
    92

    Default Two CustomDateEditor: one for Date and other for Time?

    Hi, all!

    How to register three CustomDateEditor, one for Date, other for Time and finally one for DateTime?

    I know how to register one of them (the date):
    Code:
            // convert java.util.Date
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", null));
            binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
    But how to register all three at once, without cause problem?

    Thanks,

    Any help will be appreciated!

    Gilberto

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    AFAIK, you cannot use all 3 types with one class.

    Why not make your own classes and properties editors. E.G.

    DateOnly.class and CustomDateOnlyEditor
    Time.class and CustomTimeEditor
    DateTime.class and CustomDateTimeEditor

    You could possibly use the java.sql date and time classes.

  3. #3
    Join Date
    Sep 2004
    Location
    Sacramento, CA
    Posts
    8

    Default Re: Two CustomDateEditor: one for Date and other for Time?

    I think what you're asking for is the ability to register different editors for different fields of your command object. The second parameter of registerCustomEditor specifies the field to bind to.

    See this post: http://forum.springframework.org/showthread.php?t=9970

    And an example:
    Code:
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"));
    binder.registerCustomEditor(Date.class, "dateShipped", new CustomDateEditor(dateFormat, true));
    
    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"));
    binder.registerCustomEditor(Date.class, "timeShipped", new CustomDateEditor(timeFormat, true));
    
    // etc..
    Last edited by robyn; May 19th, 2006 at 05:29 AM.

  4. #4
    Join Date
    Oct 2004
    Location
    Palmas-TO, Brazil
    Posts
    92

    Default

    Thanks, Michael!
    It is what I'm looking for! :lol:

    Gilberto

  5. #5
    Join Date
    Aug 2005
    Location
    Benetech
    Posts
    4

    Default

    I'd like to revive this topic - I face a similar problem, of supporting custom editors for a "date" and a "time". My approach was to use different classes for each, since the command object bean literally used java.util.Date for one and java.sql.Time for the other:

    Code:
        private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        private static final DateFormat timeFormat = new SimpleDateFormat("h:mm a");
    
        protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
            super.initBinder(request, binder);
            CustomDateEditor cde = new CustomDateEditor(dateFormat, true);
            binder.registerCustomEditor(java.util.Date.class, cde);
            CustomDateEditor cte = new CustomDateEditor(timeFormat, true);
            binder.registerCustomEditor(java.sql.Time.class, cte);
        }
    This actually worked when displaying the command object in a form. The date field appeared in the "date" format, the time field in the "time" format. So far so good.

    The problem arose with form processing. Submitting exactly the values Spring populated into the form (ie. "2005-08-03" for date and "12:00 AM" for time), the date converts successfully, but the time gets "Failed to convert property value of type [java.lang.String] to required type [java.sql.Time] for property 'responseTime'"

    So it would appear Spring does use the custom editors for getting both fields, but only for setting the "date" field. Any ideas why it can't convert the time?

  6. #6
    Join Date
    Aug 2005
    Location
    Benetech
    Posts
    4

    Default

    OK, I found the source of the problem. I'm using Spring 1.1 and it's at line 905 of BeanWrapperImpl.

    Simply, the CustomDateEditor for java.sql.Time almost works correctly, but produces a convertedValue that is a java.util.Date. That throws a TypeMismatchException. CustomDateEditor is hardcoded to set a Date value.

    I was able to fix it by creating a CustomTimeEditor subclass that simply re-constructs the getValue() return value:
    Code:
    public class CustomTimeEditor extends CustomDateEditor {
    
        public CustomTimeEditor(DateFormat dateFormat, boolean allowEmpty) {
            super(dateFormat, allowEmpty);
        }
    
        public Object getValue() {
            return new Time(((Date)super.getValue()).getTime());
        }
    }

  7. #7

    Default How to display error message

    Hi I register two customDateEditor, one for the time and another for the date.

    I manage to bind correctly, But I can't display the error message correctly. It's able to display the error
    for the first field, but it can't display for the other. I have 2 date fields and
    2 time fields.


    here's what I have in my jsp
    <td>Début de l'emploi</td>
    <spring:bind path="demandePersonnel.demandeEmploi.dateDebut">
    <td>
    <input type="hidden" name="_<c:out value="${status.expression}"/>">
    <input type="text" maxlength="10" size="10" name="demandeEmploi.dateDebut"

    value="<c:out value="${status.value}"/>"></input> (yyyy-mm-dd)
    </td>
    </spring:bind>
    <td>
    <spring:bind path="demandePersonnel.demandeEmploi.dateFin">
    <input type="text" maxlength="10" size="10" name="demandeEmploi.dateFin"
    value="<c:out value="${status.value}"/>">
    </input> (yyyy-mm-dd)
    </spring:bind>
    </td>

    <td>Heure de début et de fin</td>
    <spring:bind path="demandePersonnel.demandeEmploi.heureDebut">
    <td width="50%">
    <input type="text" name="demandeEmploi.heureDebut"
    value="<c:out value="${status.value}"/>"></input>

    </td>
    </spring:bind>

    <spring:bind path="demandePersonnel.demandeEmploi.heureFin">
    <td width="50%">
    <input type="text" name="demandeEmploi.heureFin"
    value="<c:out value="${status.value}"/>"></input>

    </td>
    </spring:bind>



    here's what I have in my message.

    title=Formulaire Demande de Personnel
    error.noDemande=Le numéro de demande est invalide
    error.idArrondissement=Vous devez saisir un arrondissement
    typeMismatch.demandeEmploi.dateDebut=La date de début de l'emploi n'est pas valide
    typeMismatch.demandeEmploi.dateFin=La date de fin n'est pas valide
    typeMismatch.demandeEmploi.heureDebut=l'entré de l'heure de début n'est pas valide
    typeMismatch.demandeEmploi.heurefin=l'entré de l'heure de fin n'est pas valide

    Here's what in bind initBinder method

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, "demandeEmploi.dateDebut", new CustomDateEditor(dateFormat, true));
    binder.registerCustomEditor(Date.class, "demandeEmploi.dateFin", new CustomDateEditor(dateFormat, true));
    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
    binder.registerCustomEditor(Date.class, "demandeEmploi.heureDebut", new CustomDateEditor(timeFormat, true) );
    binder.registerCustomEditor(Date.class, "demandeEmploi.heureFin", new CustomDateEditor(timeFormat, true) );

    Anyone encounter this kind of problem?

  8. #8

    Default silly me

    I found the probleme

    it's because I didn't put the message in hasBindErrors tag.

    Sorry everyone.

    the <spring:hasBindErrors name="demandePersonnel">
    <b>Please fix all errors!</b>
    <spring:bind path="demandePersonnel.noDemande">
    <font color="red"><c:out value="${status.errorMessage}"/></font>

    </spring:bind>
    <spring:bind path="demandePersonnel.demandeEmploi.dateDebut">
    <font color="red"><c:out value="${status.errorMessage}"/></font>
    </spring:bind>
    <spring:bind path="demandePersonnel.demandeEmploi.heureDebut">
    <font color="red"><c:out value="${status.errorMessage}"/></font>
    </spring:bind>
    </spring:hasBindErrors>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •