Spring MVC appears Failed to convert property value of type ' java.lang.String ' to required type ' java.util.Date ' for property ' E Ndtime '

Source: Internet
Author: User

In spring MVC, the binding page passes the time string data to the date type with an error:

Failed to convert property value of type [java.lang.String] to required type [Java.util.Date] for property ' Expert.birthda Te '; Nested exception is Java.lang.IllegalArgumentException:Cannot convert value of type [java.lang.String] to required type [ Java.util.Date] for property ' birthdate ': no matching editors or conversion strategy found

Workaround One:

1. Enable the corresponding controller controllers to inherit extends Simpleformcontroller
2. Overriding the Initbinder method  

protected void Initbinder (httpservletrequest request, Servletrequestdatabinder Binder){          new SimpleDateFormat ("Yyyy/mm/dd");             Dateformat.setlenient (false);             Binder.registercustomeditor (Date. class New  false));              }  

Note the SimpleDateFormat date format is consistent with the page date format!

Workaround Two:

Spring3.0 above Simpleformcontroller is obsolete, the latest way is to use @initbinder annotations

In the corresponding controller controllers

    @InitBinder    protectedvoid  init (httpservletrequest request, Servletrequestdatabinder binder) {        new SimpleDateFormat ("Yyyy-mm-dd");        Dateformat.setlenient (false);        Binder.registercustomeditor (Date. class New false ));    }

Spring MVC appears Failed to convert property value of type ' java.lang.String ' to required type ' java.util.Date ' for property ' E Ndtime '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.