Spring 使用 Thymeleaf 处理 DateTime 字段启动

Spring Boot with Thymeleaf handling DateTime field

我正在使用 Spring Boot v2.0.3 和 Thymeleaf。我在保留或编辑声明如下的日期字段时遇到问题:

@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "yyyy-MM-dd'T'hh:mm:ss")
@Column(name = "date_agenda", nullable = false)
@Basic(fetch = FetchType.EAGER)
@NotNull(message = "la date doit être renseignée")
@XmlElement
Date dateAgenda;

这是我的控制器:

// Save
@RequestMapping(value = "/agendaEdit", method = RequestMethod.POST)
public String agendaEdit(@Valid Agenda agenda, BindingResult bindingResult, Model model) {
    if (bindingResult.hasErrors()) {
        return "/agenda/agendaEdit";
    } else {
        agendaService.saveAgenda(agenda);
        model.addAttribute("agenda", agendaService.getAllAgenda());
        return "/agenda/agendaList";
    }
}

和我的模板:

<div class="form-group"
     th:classappend="${#fields.hasErrors('agenda.dateAgenda')} ? 'has-error'">
    <div class="col-md-3">
        <label for="content">Date</label>
        <input type="datetime-local" class="form-control" id="content" th:field="*{agenda.dateAgenda}"/>
        <p th:if="${#fields.hasErrors('agenda.dateAgenda')}"
           th:errors="*{agenda.dateAgenda}" class="help-block">Name Error</p>
    </div>
</div>

我收到以下错误:

您的问题出在 @DateTimeFormat 的模式中:您使用 "yyyy-MM-dd'T'hh:mm:ss" 解析秒数,这不是 datetime-local 的一部分输入。

改用"yyyy-MM-dd'T'hh:mm",它将按预期工作。