Results 1 to 6 of 6

Thread: Date binder don't work although CustomDateEditor is registed

  1. #1
    Join Date
    Oct 2004
    Location
    China
    Posts
    14

    Default Date binder don't work although CustomDateEditor is registed

    I now use Spring MVC to develop and meet one strange problem . I have one Command class Employee which has one property named birthDay.My
    Employee class likes :
    Employee {
    private java.util.Date birthDay;
    public void setBirthDay ( java.util.Date birthday ){
    this.birthDay = birthDay;
    }
    public java.util.Date getBirthDay ( ){
    return this.birthDay;
    }
    In my formController class AddEmployeeFormController which extends SimpleFormControlle I registed CustomDateEditor as such:
    protected void initBinder(HttpServletRequest request,
    ServletRequestDataBinder binder) throws Exception { java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy/MM/dd");
    dateFormat.setLenient(true);
    binder.registerCustomEditor(java.util.Date.class, null ,new CustomDateEditor(dateFormat, true));

    }
    my formView jsp file is:

    <tr>
    <td height="20" width = "30%" ><fmt:message key = "employee.birthday" />:</td>
    <td height="20" width = "70%" align = "left" >
    <spring:bind path="employee.birthday">
    <input name = "birthday" type = "text" value="<c:out value="${status.value}"/>">
    </spring:bind><font color = "red" >yyyy/MM/dd</font></td>
    </tr>
    when I input "2005/03/08" , it don't throw exception but say there is binding error. I print the BinderException's content:
    Data binding errors: 1

    errors.getMessage():BindException: 1 errors; Field error in object 'employee' on field 'birthday': rejectedValue=[Tue Mar 08 00:00:00 CST 2005]; codes=[errors.date.employee.birthday,errors.date.birthday ,errors.date.java.util.Date,errors.date]; arguments=[(org.springframework.context.support.DefaultMessag eSourceResolvable)[codes=[employee.birthday]; arguments=[null]; defaultMessage=[employee.birthday]], (java.lang.String)[yyyy/MM/dd]]; defaultMessage=[errors.date].
    I really don't what's wrong with this. Who can help me ? Thanks very much.
    Although I study carefully the content of :http://forum.springframework.org/viewtopic.php?t=537. I can't
    get out some solution.

  2. #2
    Join Date
    Oct 2004
    Location
    China
    Posts
    14

    Default It seems that my validator can't work when validating

    My validator is org.springframework.validation.commons.BeanValidat or .
    When I set my AddEmployeeFormController's validateOnBinding to false,
    the problem disappear . It seems that the validator can't validate my
    birthDay property after binding . Anyone meet the same case?

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

    Default

    Try:
    Code:
    <spring&#58;bind path="command.employee.birthDate">
    	<input type="text" name="<c&#58;out value="$&#123;status.expression&#125;"/>" value="<c&#58;out value="$&#123;status.displayValue&#125;"/>" />
    	<span class="inputError"><c&#58;out value="$&#123;status.errorMessage&#125;"/></span>
    </spring&#58;bind>

  4. #4
    Join Date
    Oct 2004
    Location
    China
    Posts
    14

    Default thanks for your reply!

    Thanks for your reply! I try to use your code .But the problem still stand
    there . I think that the problem is not CustomCustomDateEditor but the validator . When I input something as "2005/01/01" , the binding success and create one Date Objcet . Then the validator validate the Date Objcet
    .Of cource the Date Object don't follow the pattern "yyyy/MM/dd" then the problem appear.I can't konw how to resolve it.Of cource if i don't validate
    the birthday property ,the problem disappear.But this isn't what I want.

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

    Default

    Sorry, just noticed you are using org.springframework.validation.commons.BeanValidat or. Haven't tried this - it's still in the sandbox. Don't suppose you can get away with using org.springframework.validation.Validator just to check dates.

  6. #6
    Join Date
    Oct 2004
    Location
    China
    Posts
    14

    Default I use other solution!

    I still use common validator to validate other properties but birthday property and depend on CustomDateEditor to validate birthday. If I input error date format :"20030301", I catch binding exception to informate me something wrong . It does work .My msn is:scqlbw@hotmail.com.Hope to meet you at msn .

Similar Threads

  1. Replies: 17
    Last Post: Jan 2nd, 2007, 01:43 PM
  2. Date format using JDBC Template
    By macmeureg in forum Data
    Replies: 5
    Last Post: Jun 29th, 2005, 10:18 AM
  3. Replies: 3
    Last Post: Apr 5th, 2005, 09:20 PM
  4. Date Field Validation
    By sherihan in forum EJB
    Replies: 0
    Last Post: Feb 7th, 2005, 04:58 AM
  5. Date Property Editors
    By smv in forum Web
    Replies: 2
    Last Post: Aug 29th, 2004, 01:50 PM

Posting Permissions

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