【问题标题】:ThymeLeaf time issue with Spring boot春季启动的百里香时间问题
【发布时间】:2019-01-05 15:43:17
【问题描述】:

我遇到了一个相当简单的问题。 我有时间的 html 字段。我想从用户输入中节省数据库中的时间。

我的 html 输入:

<input type="time" th:field="*{startTime}"/>

我在模型类中尝试了不同的方法。

import java.time.LocalDateTime;
.....
@Temporal(TemporalType.TIME)
@DateTimeFormat(iso = ISO.TIME)
private LocalDateTime startTime;

这会在启动应用程序时导致错误:@Temporal 只能在 java.util.Date 或 java.util.Calendar 上设置。

我尝试先删除@Temporal,然后再删除

import java.sql.Time;
....
private Time startTime;

在保存表单的两种情况下,我都会收到错误消息:object='process' 的验证失败。错误计数:1

我指的是这篇文章中接受的答案 - Spring boot / Thymeleaf - Getting Time from input

请帮忙。

更新:pom.xml 依赖和整个错误

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.3.3.Final</version>
        </dependency>
    </dependencies> 

Whitelabel 错误页面 此应用程序没有明确的映射 /error,因此您将其视为后备。

Sun Jul 29 02:27:39 CEST 2018 There was an unexpected error (type=Bad 请求,状态=400)。 object='process' 的验证失败。错误 计数:1

【问题讨论】:

  • 这不是 Thymeleaf 问题,而是 Hibernate 问题。如果您使用包含的 Hibernate 依赖项,则可能需要包含 hibernate-java8 依赖项以获得时间支持。
  • @chrylis:感谢您的回复。但是你确定吗?虽然已弃用,但我只是尝试同时添加 hibernate-java8 和建议的 hibernate-core 依赖项。仍然出现同样的错误。
  • 发布您的 POM 文件(以及您的整个堆栈跟踪,而不仅仅是摘要行);例如,发生了什么验证错误?
  • @chrylis:已更新。我在表单中有其他文本字段,仅在添加此时间字段后我才收到此错误。也有一个 java.sql.Date 字段,除了时间字段之外的所有字段都可以正常工作。
  • 当您获得“白标”页面时,您还应该在 Java 控制台上获得更详细的错误报告。

标签: java spring spring-boot thymeleaf


【解决方案1】:

目前,我已经“解决”了它使用

<input type="datetime-local" th:field="*{time}"/>

在模型类中

@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm")
private LocalDateTime time;

我提到了这篇文章中给出的第一个答案 - Spring with Thymeleaf binding date in html form

【讨论】:

  • 显然,您只需要时间。那为什么要使用LocalDateTime 而不是LocalTime
猜你喜欢
  • 1970-01-01
  • 2020-01-25
  • 2018-05-19
  • 2020-10-23
  • 2015-12-09
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
  • 2020-07-02
相关资源
最近更新 更多