java - 带有 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]**
这是为什么?
解决方案
仔细看看这个:
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
推荐阅读
- python - 在seaborn / matplotlib中填充两个正态分布之间的重叠区域
- oauth-2.0 - 您能否解释一下 PKCE 的 oauth2 授权代码流如何工作以及为什么它为浏览器客户端提供更高的安全性?
- jekyll - 我可以为 Jekyll 博客设置单独的类别 slug 和标题吗?
- amazon-web-services - Amazon Connect 出站呼叫 - 寻求示例代码链接
- javascript - 如何创建像 jsfiddle 一样的所见即所得
- javascript - 谷歌脚本 indexOf 不适用于自定义元素
- keras - 强化学习——如何教神经元网络避免在剧集中已经选择的动作?
- c# - 使用实体框架将通用复杂属性保存为序列化列
- c++ - windows.h 中的虚拟键码
- node.js - NGINX 只传递来自主域的请求