2015-09-10 44 views
4

我有一個Java時間屬性綁定到Thymeleaf字段的問題。如何將Java Spring time屬性綁定到Thymeleaf字段?

這裏是我的HTML代碼

<input th:field="*{startTime}" type="text"/> 
    <script> 
$("#startTime").timepicker({ 
      showSeconds : true, 
      showMeridian : false, 
      secondStep : 1 

     }); 
    </script> 

這裏是我的模型屬性代碼

@NotNull(message = "Start time cannot be empty") 
    private Time startTime; 

public Time getStartTime() { 
     return startTime; 
    } 

    public void setStartTime(Time startTime) { 
     this.startTime = startTime; 
    } 

當我提交我得到異常

 
Failed to convert property value of type java.lang.String to required 
type java.sql.Time for property startTime; nested exception is 
org.springframework.core.convert.ConversionFailedException: Failed to 
convert from type java.lang.String to type 
@javax.validation.constraints.NotNull 
@org.springframework.format.annotation.DateTimeFormat java.sql.Time 
for value 14:15:41; nested exception is 
org.joda.time.IllegalFieldValueException: Cannot parse "14:15:41": 
Value 14 for clockhourOfHalfday must be in the range [1,12] 

形式。如果我插入12小時我有這個例外

 
Failed to convert property value of type java.lang.String to required type java.sql.Time for property startTime; nested exception is 
org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @javax.validation.constraints.NotNull 
@org.springframework.format.annotation.DateTimeFormat java.sql.Time for value 11:15:41; nested exception is 
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type org.joda.time.DateTime to type @javax.validation.constraints.NotNull @org.springframework.format.annotation.DateTimeFormat java.sql.Time 

如何將Spring Time屬性綁定到Thymeleaf字段?

回答

0

創建屬性類型爲String,並創建一套獲取方法和綁定查看

private String startTimeString; 

和控制器側轉換值的字符串時,並設置它

setStartTime(Time.valueOf(ModelName.getstartTimeString())); 
相關問題