我使用thymeleaf
並在我的項目,我需要保存date
和mySql
數據庫 time
對象,這是我如何驗證我的實體類如何得到有效的日期和使用彈簧時刻和thymeleaf
@NotNull
@Column(name = "sdate")
@DateTimeFormat(iso = ISO.DATE)
private Date sdate; //represents start Date - i am passing 2014-01-01- this works fine.
@NotNull
@Column(name = "stime")
@DateTimeFormat(iso = ISO.TIME)
private Date stime; //represent start time - i am passing 12:10:20.444
當我試圖從thymleaf
形式的值到controller
我不能得到一個valid
類它包含errors
。有沒有我做錯了validation
部分。我怎麼能從form
得到valid input
。
public String save(@ModelAttribute("travel") @Valid Travel travel,BindingResult result){
if (result.hasErrors()) {
for (Object element : result.getAllErrors()) {
System.out.println(element.toString());
}
}
錯誤消息:
Field error in object 'travel' on field 'stime': rejected value [12:10:20.444]
你試過['pattern()'](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/format/annotation/DateTimeFormat.html#pattern--)自定義日期時間格式? – Lucky