简体   繁体   中英

ThymeLeaf time issue with Spring boot

I am stuck with a rather so called simple issue. I have html field for time. I want to save the time in database from user input.

my html input:

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

I tried with different approach in model class.

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

This results in error while starting the application : @Temporal should only be set on a java.util.Date or java.util.Calendar.

I tried with first removing the @Temporal, and then with

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

In both occasion upon saving the form I am getting error : Validation failed for object='process'. Error count: 1

I was referring to the accepted answer in this post - Spring boot / Thymeleaf - Getting Time from input

Kindly help.

UPDATE : the pom.xml dependencies and entire error

<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 Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Jul 29 02:27:39 CEST 2018 There was an unexpected error (type=Bad Request, status=400). Validation failed for object='process'. Error count: 1

For the time being, I have 'solved' it using

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

And in the model class

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

I referred the first answer given in this post - Spring with Thymeleaf binding date in html form

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM