首页 > 解决方案 > 带有 LocalDateTime 问题的 Spring Thymeleaf

问题描述

我在使用 Spring 和 Thymeleaf 时遇到了 localdatetime 属性问题。

我的代码:

事件.java

public class Event {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable=false)
    @NotNull(message="Name is required!")
    private String name;

    @Column(nullable=false)
    @NotNull(message="Time is required!")
    @DateTimeFormat(pattern = "MM.dd.yyyy. HH:mm")
    private LocalDateTime time;
}

事件控制器.java

...
@GetMapping("/eventEntry")
public String showForm(Model model) {

    model.addAttribute("event", new Event());

    return "eventEntry";
}

@PostMapping("/eventEntry")
public String processForm(@RequestParam("time") @DateTimeFormat(pattern = "MM.dd.yyyy. HH:mm") LocalDateTime time, 
        @Valid Event event, Errors errors, Model model) {

    if(errors.hasErrors()) {

        return "eventEntry";

    } else {

        eventList.add(event);

        eventRepository.save(event);

        model.addAttribute("event", event);
        model.addAttribute("time", time);
        model.addAttribute("listaDogadaja", listaDogadaja);

        return "output";
    }
}

eventEntry.html

<body>
    <h1>Event entry form</h1>

    <h3>New event</h3>

    <form method="POST" th:object="${event}">

        <div class="form-group">
            <label for="naziv">Name: </label>
            <input type="text" th:field="*{name}" />
            <span class="validation-error" th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</span>
        </div>

        <div class="form-group">
            <label for="time">Time: </label>
            <input type="datetime-local" th:field="*{time}" />
            <span class="validation-error" th:if="${#fields.hasErrors('time')}" th:errors="*{time}">Time Error</span>
        </div>

        <div class="form-group">
            <input type="submit" th:value="Save">
            <input type="reset" class="btn btn-danger" th:value="Cancel">
        </div>

    </form>
</body>

单击保存按钮时,我收到此异常:

Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDateTime';
nested exception is 
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value '05.14.2014. 1:00 PM';
nested exception is
java.lang.IllegalArgumentException: Parse attempt failed for value [05.14.2014. 1:00 PM]**

这是为什么?

标签: javaspringspring-bootdatetimethymeleaf

解决方案


仔细看看这个:

java.lang.IllegalArgumentException: Parse attempt failed for value [05.14.2014. 1:00 PM]**

提供的时间是:05.14.2014。1:00 PM

您需要相应地更改代码以支持AM/PM:尝试:

...
@DateTimeFormat(pattern = "MM.dd.yyyy. HH:mm a")
private LocalDateTime time;

...
public String processForm(@RequestParam("time") @DateTimeFormat(pattern = "MM.dd.yyyy. HH:mm a") LocalDateTime time, 

这里a表示 AM 或 PM。

更多请看这里:format-time-12-hours-pattern


推荐阅读