我正在使用Spring MVC和Thymeleaf。这是日期时间选择器的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>
在控制器中我有:
@RequestMapping(value ="/created",method = RequestMethod.POST)
public String submitNewCustomer(@ModelAttribute Customer customer){
customerService.createNewCustomer(customer);
return "edit";
}
客户类如下:
@Data
@Entity
public class Customer {
@Id
@GeneratedValue
Long id;
String firstname;
String lastname;
@Temporal(TemporalType.TIMESTAMP)
Date dateOfBirth;
String username;
String password;
}
很不幸,当我提交表单时,它报错了:
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]
那么,我该怎么修复它呢?