简体   繁体   English

无法将属性“ dateOfBirth”的类型“ java.lang.String”的属性值转换为所需的类型“ java.util.Date”;

[英]Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'dateOfBirth';

I am using spring mvc and Thymeleaf. 我正在使用spring mvc和Thymeleaf。 Here is the html form for datetime-picker: 这是datetime-picker的html表单:

<form action="#" th:action="@{/created}" th:object="${customer}" method="post" class="form-horizontal">    
<div class="row edit-form">
        <label for="name" class="col-sm-2 control-label text-right">Date of Birth</label>
        <div class="col-sm-6">
            <input type="date" class="form-control"    
            th:field="*{dateOfBirth}" th:required="required" id="dateOfBirth"/>
        </div>
    </div>
</form>

In the controller i have: 在控制器中,我有:

  @RequestMapping(value ="/created",method = RequestMethod.POST)
        public String  submitNewCustomer(@ModelAttribute Customer customer){
            customerService.createNewCustomer(customer);
            return "edit";
        }

and the Customer class is: 客户类是:

@Data
@Entity
public class Customer {

    @Id
    @GeneratedValue
    Long id;

    String firstname;
    String lastname;
@Temporal(TemporalType.TIMESTAMP)
    Date dateOfBirth;
    String username;
    String password;

}

Unfortunately, when i submit the form it complains with: 不幸的是,当我提交表格时,它抱怨:

Field error in object 'customer' on field 'dateOfBirth': rejected value [2016-12-14]; codes [typeMismatch.customer.dateOfBirth,typeMismatch.dateOfBirth,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [customer.dateOfBirth,dateOfBirth]; arguments []; default message [dateOfBirth]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'dateOfBirth'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.persistence.Temporal java.util.Date] for value '2016-12-14'; nested exception is java.lang.IllegalArgumentException]

So, how can i fix it? 那么,我该如何解决?

The issue is solved when I changed the class to: 当我将类更改为:

@DateTimeFormat(iso=ISO.DATE)
Date dateOfBirth;

It seems that, it helps to convert string to more complex format like date. 看来,这有助于将字符串转换为更复杂的格式,例如date。 See here . 这里

You need to use the @Temporal annotation 您需要使用@Temporal批注

@Temporal(TemporalType.TIMESTAMP)
private java.util.Date dateOfBirth;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型&#39;java.lang.String&#39;的属性值转换为必需类型&#39;java.util.Date&#39; - Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' 无法将 java.lang.String 类型的属性值转换为所需类型 java.util.Date - Failed to convert property value of type java.lang.String to required type java.util.Date 无法将属性fromDate的java.lang.String类型的属性值转换为所需的java.util.Date类型; - Failed to convert property value of type java.lang.String to required type java.util.Date for property fromDate; 错误:无法将类型java.lang.String的属性值转换为属性dob的必需类型java.util.Date? - Error: Failed to convert property value of type java.lang.String to required type java.util.Date for property dob? 使用@DateTimeFormat(pattern =“ HH:mm”)无法将类型[java.lang.String]的属性值转换为所需的类型[java.util.Date] - Failed to convert property value of type [java.lang.String] to required type [java.util.Date] with @DateTimeFormat(pattern = “HH:mm”) 无法将类型“java.lang.String”的属性值转换为属性“日期”所需的类型“java.util.Date”:它不完全是 10 个字符长 - Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date': it is not exactly10characters long 无法将“java.lang.String”类型的值转换为所需类型“java.util.Date” - Failed to convert value of type 'java.lang.String' to required type 'java.util.Date' 无法将 java.lang.String 类型的值转换为所需的 java.util.Date 类型 - Failed to convert value of type java.lang.String to required type java.util.Date [无法将类型&#39;java.lang.String []&#39;的属性值转换为属性的必需类型&#39;java.util.List&#39; - [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.List' for property 无法将 java.lang.String[] 类型的属性值转换为所需的类型 java.util.List - Failed to convert property value of type java.lang.String[] to required type java.util.List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM