java - 如何将 Java Spring 时间属性绑定(bind)到 Thymeleaf 字段?

标签 java spring time thymeleaf

我在将 Java 时间属性绑定(bind)到 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 属性绑定(bind)到 Thymeleaf 字段?

最佳答案

创建一个String类型的Property并创建get set方法并绑定(bind)到view

private String startTimeString;

Controller 端将值字符串转换为时间并设置

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

关于java - 如何将 Java Spring 时间属性绑定(bind)到 Thymeleaf 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32497464/

相关文章:

java - 在maven中聚合父pom的所有来源

java - 如何在单个查询对象 Mongodb Spring 数据中添加 or & 和 Criteria 子句

linux - 关于linux内核jiffies

sql-server - SQL将int转换为时间

java - 找到最长的字符串前缀

java - 安卓 View 布局。让 View 填充 View 之间的空间

java - 部署到 sonatype 时为 "Received fatal alert: bad_record_mac"

java - 如何禁用 undertow 的自动配置

java - JMSXGroupId 的 RabbitMQ 解决方法

php - (php) 日期和时间