首页 > 解决方案 > 如何将文本输入转换为 java.time.LocalDateTime?

问题描述

我正在尝试制作表格,这将为我的大学数据库添加新的讲座。因此,模型类 Lecture 具有 LocalDateTime 日期字段,我需要将输入文本从表单转换为 LocalDateTime,我遇到了问题:

Failed to convert property value of type java.lang.String to required type java.time.LocalDateTime for property date; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalDateTime]

我的控制器:

@GetMapping("/createNew")
    public String showSignUpForm(Model model) {
        Lecture lecture = new Lecture();
        model.addAttribute("lecture", lecture);
        return "lecture/SaveLecture";
    }

@PostMapping("/addLecture")
public String createLecture(@Valid Lecture lecture, BindingResult result, Model model) {
    if (result.hasErrors()) {
        return "lecture/SaveLecture";
    }

    try {
        lectureService.save(lecture);
    } catch (TimeTableOverlayException e) {
        return "lecture/SaveLecture";
    }

    return this.getAllLectures(model);
}

和形式:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<title>Save new lecture</title>
</head>
<body>
<div>
<form action="#" th:action="@{/lecture/addLecture}" th:object="${lecture}" method="POST">
    ...
    <label for="date">Date</label>
    <input type="text" th:field="*{date}" id="date" placeholder="Date&Time">
    <span th:if="${#fields.hasErrors('date')}" th:errors="*{date}"></span> 
    <br/>
    ...
    <input type="submit" value="Add lecture">
</form>
</div>
</body>
</html>

我的讲座课:

public class Lecture {

    private int id;
    private List<Group> groups = new ArrayList<>();
    private Teacher teacher;
    private Subject subject;
    private LocalDateTime date;
    private Audience audience;

    public Lecture(int id) {
        this.id = id;
    }

    public Lecture() {

    }

    public Lecture(Teacher teacher, Subject subject, LocalDateTime date) {
        this.teacher = teacher;
        this.subject = subject;
        this.date = date;
    }

    public Lecture(int id, Teacher teacher, Subject subject, List<Group> groups, LocalDateTime dateTime) {
        this.id = id;
        this.teacher = teacher;
        this.subject = subject;
        this.groups = groups;
        this.date = dateTime;
    }

    public Lecture(int id, Teacher teacher, Subject subject, LocalDateTime dateTime) {
        this.id = id;
        this.teacher = teacher;
        this.subject = subject;
        this.date = dateTime;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setGroups(List<Group> groups) {
        this.groups = groups;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    public void setSubject(Subject subject) {
        this.subject = subject;
    }

    public void setDate(LocalDateTime dt) {
        this.date = dt;
    }

    public void setAudience(Audience audience) {
        this.audience = audience;
    }

    public List<Group> getGroups() {
        return groups;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public Subject getSubject() {
        return subject;
    }

    public LocalDateTime getDate() {
        return date;
    }

    public Audience getAudience() {
        return audience;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((audience == null) ? 0 : audience.hashCode());
        result = prime * result + ((date == null) ? 0 : date.hashCode());
        result = prime * result + ((groups == null) ? 0 : groups.hashCode());
        result = prime * result + id;
        result = prime * result + ((subject == null) ? 0 : subject.hashCode());
        result = prime * result + ((teacher == null) ? 0 : teacher.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Lecture other = (Lecture) obj;
        if (audience == null) {
            if (other.audience != null)
                return false;
        } else if (!audience.equals(other.audience))
            return false;
        if (date == null) {
            if (other.date != null)
                return false;
        } else if (!date.equals(other.date))
            return false;
        if (groups == null) {
            if (other.groups != null)
                return false;
        } else if (!groups.equals(other.groups))
            return false;
        if (id != other.id)
            return false;
        if (subject == null) {
            if (other.subject != null)
                return false;
        } else if (!subject.equals(other.subject))
            return false;
        if (teacher == null) {
            if (other.teacher != null)
                return false;
        } else if (!teacher.equals(other.teacher))
            return false;
        return true;
    }
}

我如何将其转换为 LocalDateTime?我看到在带有注释@DateTimeFormat 的控制器中转换的例子,但我不明白如何在我的情况下使用它......

标签: javahtmlspringthymeleaflocaldatetime

解决方案


删除placeholder="Date&Time". 并使用默认日期:

@GetMapping("/createNew")
    public String showSignUpForm(Model model) {
        Lecture lecture = new Lecture();
        lecture.setDate(LocalDateTime.now())
        model.addAttribute("lecture", lecture);
        return "lecture/SaveLecture";
    }

推荐阅读