spring - 无法将 'java.lang.String' 类型的属性值转换为 thymeleaf 形式的属性 'java.time.LocalDate' 所需的类型 'date'

标签 spring kotlin thymeleaf java-time

<form th:action="@{/hi}"  th:object="${datum}" method="post">
    <p>date : <input type="date" th:field="*{date}"/> </p>
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

上述表单的 Controller

@PostMapping("/hi")
fun testum(@ModelAttribute datum: Datum) {
    println(datum)
}

简单的pojo类

class Datum(
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        var date: LocalDate? = null
)

我试图以表单发送日期,但出现此异常:

Resolved exception caused by Handler execution: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'datum' on field 'date': rejected value [2018-06-20]; codes [typeMismatch.datum.date,typeMismatch.date,typeMismatch.java.time.LocalDate,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [datum.date,date]; arguments []; default message [date]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDate' for property 'date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalDate] for value '2018-06-20'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2018-06-20]]

但是如果我将类型从 LocalDate 更改为 String 它工作正常。我想将表单中的日期映射到 Datum 类的日期属性。谁能帮我吗?有链接吗?谢谢。

此链接对我没有帮助similar problem

最佳答案

这是一个老问题,但我为 future 的读者留下了一个答案。
这不是 thymeleaf 的问题,更多的是 @DateTimeFormat 绑定(bind)的问题。
如果 Datum 类按如下方式更改,则上述问题中的示例将起作用:

解决方案 1。

class Datum {
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    var date: LocalDate? = null
}

请注意,date 字段已从主构造函数中的声明移至类主体中。
(请注意从 Datum (...)Datum {...} 的更改)

解决方案 2。

class Datum (
   @field:DateTimeFormat(pattern = "yyyy-MM-dd") var date: LocalDate? = null
)

如果由于将其作为数据类或其他原因需要在构造函数内声明它,则必须使用use-site targets进行注释.
但是,请注意解决方案 2 可能并不总是有效。我无法在示例项目中重现 - 但在现实项目中,存在 @field:DateTimeFormat 未正确将请求参数字符串绑定(bind)到 Date 的问题> 对象。它有时有效,有时无效,使得调试非常棘手。
当它不起作用时,它会抛出诸如 Validation failed for object='Datum' 之类的错误。错误计数:1,而恢复到解决方案 1 却始终有效。每次我们再次编译时,解决方案 2 有时会起作用,但在不更改任何代码的情况下会随机崩溃。
我们采取严格地将 @DateTimeFormat 带注释的字段放入类主体声明中以确保其有效。

关于spring - 无法将 'java.lang.String' 类型的属性值转换为 thymeleaf 形式的属性 'java.time.LocalDate' 所需的类型 'date',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50747612/

相关文章:

android - 在 fragment 中更新 RecyclerView 时的 scrollToPosition()

java - 错误:Neither BindingResult nor plain target object for bean name 'id' available as request attribute

java - Spring 注入(inject)中的循环依赖 - 这是糟糕的设计吗?

java - Spring:如何在RESTful服务中为@RequestBody添加XSS保护?

android - 使用 Jetpack Compose 的深层链接导航到可组合项

kotlin - Aspectj 不适用于 kotlin

java - 使用 Spring MVC 和 thymeleaf 的“下一个”/“上一个”按钮更改列表中的对象

html - 如何使用 thymeleaf 在 div 中使用 if 语句?

java - 如何用 MyBatis/Spring 实现批量操作?

java - Spring AbstractMongoEventListener 不触发