首页 > 解决方案 > 注释 @DateTimeFormat 不适用于 Spring boot 和 Thymeleaf

问题描述

我正在使用 Springboot、Java 和 Thymeleaf。

public class DirectBind {

    @Column(columnDefinition = "date")
    @DateTimeFormat(pattern = "MM/dd/yyyy")
    private LocalDate policyTermDateStart;

    @Column(columnDefinition = "date")
    @DateTimeFormat(pattern = "MM/dd/yyyy")
 ...
}

我的日期即将到来 yyyy-mm-dd。你知道我如何改变这个/在哪里实现代码,所以它会改变。它进入我的控制器吗?这是我发送获取用户输入日期的表单时的代码

@RequestMapping(value = "/send")
public String send(Model model, @ModelAttribute(value = "directBind") DirectBind directBind) {
    List<String> businessAgencyList = directBind.getBusinessAgencyList();
    List<String> billTypeOptions = directBind.getBillTypeOptions();
    Mail mail = new Mail();
    mail.setFrom("no-reply@hgitservices.com");
    mail.setTo(new String[]{"stacief@hgitservices.com"});
    mail.setSubject("Oli Affiliate - AMS360 & PMA Data Checklist");
    Map<String, Object> mailModel = new HashMap<>();
    mail.setModel(mailModel);
    try {
        emailService.sendSimpleMessage(mail, directBind);
    } catch (Exception e) {
        e.printStackTrace();
        return ("redirect:/?sentMessageFail");
    }
    return ("redirect:/?sentMessage");
}

@RequestMapping(value = "/email")
public String email() {
    return "emailMessage";
}

标签: javadatespring-bootthymeleafdate-formatting

解决方案


使用#temporals 让它工作!

<tr class="emailRow">
        <td colspan="1" class="dateRangeEnd" th:text="${#temporals.format(directBind.policyTermDateEnd, 'MM/dd/yyyy')}">
         </td>
</tr>


推荐阅读