2016-05-17 21 views
-1

我使用thymeleaf並在我的項目,我需要保存datemySql數據庫 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] 
+0

你試過['pattern()'](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/format/annotation/DateTimeFormat.html#pattern--)自定義日期時間格式? – Lucky

回答

0

根據spring document for DateTimeFormat.ISO.TIME,用於ISO.TIME正確的格式是 「12:10:20.444 + 00:00」,其中 「00:00」 的部分是時區偏移量(假設您的時區沒有偏移量)。

因此,您需要重新格式化您的輸入以在您的時間字符串中包含時區偏移量。

+0

謝謝,但沒有奏效。 – Priyamal

+0

您可以請發佈新的錯誤消息,謝謝 – DoT

+0

我收到相同的錯誤。 – Priyamal

相關問題