Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Binding Composite Date Fields

  1. #1
    Join Date
    May 2006
    Posts
    7

    Default Binding Composite Date Fields

    I have a form with 3 select boxes (day, month & year). I need to bind these fields to a single Date field in my command object. What is the best practice for doing this?

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

    Default

    You can make your own date form object with each field (day, month & year) as a nested property. This allows you to valid the fields individually or as a group.

    Another option would be to combine them into a single string in Javascript, and submit that.

  3. #3
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Or make use of the deprecated methods (shock horror ) of the existing java.util.Date (get/SetDay|Month|Year).

  4. #4
    Join Date
    May 2006
    Location
    Darmstadt - Germany
    Posts
    142

    Default

    i have this broblem and, if possible, i would like to see, please, some example of solution... i have the following:
    Code:
    <Spring:bind path="fxRate.rpDate">
     <input type="text" name="rpDate" size="10" value="${status.value}" />
    </Spring:bind>
    the date is shown, like yyyy-mm-dd... without any problem, but when this field is modificated, the onSubmid method is not called because, i think, of some validation of spring, that cannot "translate" this new yyyy-mm-dd in Date again... can u plz help me with that?
    thks a lot

  5. #5
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Have you registered a property editor against your date field?

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

    Default

    if possible, i would like to see, please, some example of solution
    From AbstractClinicForm in the PetClinic example:
    Code:
        protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            dateFormat.setLenient(false);
            binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
            binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
        }

  7. #7
    Join Date
    May 2006
    Location
    Darmstadt - Germany
    Posts
    142

    Default

    thks for replies!!

    Quote Originally Posted by yatesco
    Have you registered a property editor against your date field?
    yes :o

    Quote Originally Posted by katentim
    From AbstractClinicForm in the PetClinic example:
    Code:
       
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            dateFormat.setLenient(false);
            binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
            binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
    }
    not working :_-(

    Code:
    public void initBinder(HttpServletRequest req,ServletRequestDataBinder binder) throws Exception{
     SimpleDateFormat specFormat = new SimpleDateFormat("yyyy-MM-dd");
     CustomDateEditor specEditor = new CustomDateEditor(specFormat, true);
     binder.registerCustomEditor(Date.class, "rpDate", specEditor);
    }
    this one too..

    may be is there a problem with java.sql.Date class? do i need to use java.util.Date ??
    when i just do not show this field (it is pre-setted earlier in formBackingObject() method), all works perfecly! (but SOMETIMES this pre-setted date needs to be modified)
    plz help :o my chef is starting to look eagly at me about this problem... thks again!
    Last edited by orusso; Jun 28th, 2006 at 05:11 AM.

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

    Default

    do i need to use java.util.Date ?
    Yes. CustomDateEditor is a property editor for java.util.Date.

  9. #9
    Join Date
    May 2006
    Location
    Darmstadt - Germany
    Posts
    142

    Default

    Quote Originally Posted by katentim
    Yes. CustomDateEditor is a property editor for java.util.Date.
    but all java.sql.Date is java.util.Date... why it was not working? ( now it is :-D, i have modified the field extencion in POJO )
    but now on ViewAll page, the date appear like "2006-06-28 00:00:00.0" .... with java.sql.Date it was "2006-06-28", a little bit more user-view-friendly... there is no initBinder() for the normal Controller interface (my viewAll-controller implements Controller)... do u plz have any suggestion for me? thks
    Last edited by orusso; Jun 30th, 2006 at 05:43 AM.

  10. #10
    Join Date
    May 2006
    Posts
    1

    Default

    Quote Originally Posted by katentim View Post
    You can make your own date form object with each field (day, month & year) as a nested property. This allows you to valid the fields individually or as a group.
    I have the same issue, 3 fields: month, day, year and wanting to combine them into one date. I like the above solution, but I don't know if it really solves the original problem of how to bind 3 fields into one java.util.Date object. Any suggestions?

    Thanks,
    Tim

Posting Permissions

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