加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何将Java Spring time属性绑定到Thymeleaf字段?

发布时间:2020-12-15 02:09:33 所属栏目:Java 来源:网络整理
导读:我有一个 Java时间属性绑定到Thymeleaf字段的问题. 这是我的HTML代码 input th:field="*{startTime}" type="text"/script $("#startTime").timepicker({ showSeconds: true,showMeridian: false,secondStep: 1 });/script 这是我的模型属性代码 @NotNull(mes
我有一个 Java时间属性绑定到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字段?

解决方法

创建一个Property type String并创建get set方法并绑定到view

private String startTimeString;

和控制器端将值String转换为Time并设置它

setStartTime(Time.valueOf(ModelName.getstartTimeString()));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读