【问题标题】:Annotation @DateTimeFormat not working with Spring boot and Thymeleaf注释 @DateTimeFormat 不适用于 Spring boot 和 Thymeleaf
【发布时间】:2018-09-26 17:07:49
【问题描述】:

我正在使用 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";
}

【问题讨论】:

  • 弹簧还是弹簧靴?
  • 对不起,Springboot。
  • 此外,如果您有两个问题,您应该将它们作为两个单独的问题提出。因此,正如标题所示,我认为主要问题是后者,而具有 date range 的问题应该在单独的问题中。
  • @DateTimeFormat 将请求参数并将字符串“12/02/2012”转换为 LocalDate 对象。当您说“日期即将发布 yyyy-mm-dd”时,您指的是收到的电子邮件吗?问题可能与您的电子邮件服务序列化日期的方式有关。
  • 是的,我稍后会将数据通过电子邮件发送给自己。它可能是电子邮件服务序列化。嗯..

标签: java date spring-boot thymeleaf date-formatting


【解决方案1】:

使用#temporals 让它工作!

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

【讨论】:

    猜你喜欢
    • 2014-08-31
    • 2018-07-04
    • 2016-10-26
    • 2019-04-11
    • 1970-01-01
    • 2020-08-15
    • 2021-01-17
    • 2017-11-28
    • 2017-06-09
    相关资源
    最近更新 更多