【发布时间】:2017-11-26 13:20:48
【问题描述】:
这是我的 html 表单的一部分。
<form th:object="${applyForm}" class="form-horizontal" role="form" method="post" >
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1"> Start Date </label>
<div class="col-sm-9">
<input type="date" id="form-field-1" th:field="*{startDate}"
placeholder="Completion Date- Estimated"
class="col-xs-10 col-sm-7" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1"> Completion Date </label>
<div class="col-sm-9">
<input type="date" id="form-field-1" th:field="*{completionDate}"
placeholder="Completion Date- Estimated"
class="col-xs-10 col-sm-7" />
</div>
</div>
</form>
我正在使用 Lombak。
public class ApplicationForm implements Serializable {
private static final long serialVersionUID = -2961891549847290936L;
@Column(name = "start_date", nullable = false)
private ZonedDateTime startDate;
//@DateTimeFormat(pattern = "yyyy-mm-dd")
@Column(name = "completion_date", nullable = false)
private ZonedDateTime completionDate;
}
这是我尝试提交表单时的错误。我试过使用@DateTimeFormat,但我仍然无法摆脱这个错误。我正在使用 spring 和 hibernate。
Field error in object 'applyForm' on field 'commencementDate': rejected value [2017-11-23]; codes [typeMismatch.applyForm.commencementDate,typeMismatch.commencementDate,typeMismatch.java.time.ZonedDateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [applyForm.commencementDate,commencementDate]; arguments []; default message [commencementDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.ZonedDateTime' for property 'commencementDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.persistence.Column java.time.ZonedDateTime] for value '2017-11-23'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2017-11-23]]
Field error in object 'applyForm' on field 'completionDate': rejected value [2017-07-30]; codes [typeMismatch.applyForm.completionDate,typeMismatch.completionDate,typeMismatch.java.time.ZonedDateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [applyForm.completionDate,completionDate]; arguments []; default message [completionDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.ZonedDateTime' for property 'completionDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.persistence.Column java.time.ZonedDateTime] for value '2017-07-30'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2017-07-30]]
【问题讨论】:
-
只需使用适当的类型:您要向用户询问日期(年 + 月 + 日)。不是带有时区的纳秒级精确瞬间。所以使用 LocalDate,而不是 ZonedDateTime。
-
尽管我更改为 LocalDate,但仍然出现同样的错误。当我将其更改为日期时,错误消失了。非常感谢!
标签: spring hibernate spring-mvc jackson