将 HTML 输入日期映射到 Java 对象的 LocalDate
Map HTML input date to LocalDate of Java Object
我有一个输入字段(类型:'date')- 谁可以使用 Thymeleaf 将它映射到我的对象中的 'LocalDate' 字段?
对象
public class Project {
@Id
private int id;
private LocalDate startDate;
private LocalDate endDate;
}
HTML 输入
<form action="#"
th:action="@{|/admin/projects/add/save|}"
th:object="${newProjects}"
method="POST"
class="form-horizontal">
<input type="date" class="form-control" id="startDate"
placeholder="Project start"
th:field="*{startDate}"/>
<input type="date" class="form-control" id="endDate"
placeholder="Project start"
th:field="*{endDate}"/>
</form>
如何将输入字段正确映射到 LocalDate startDate 或 endDate?
控制器
//GetMapping for Projects is also there, but I didn't paste it to keep clarity
@PostMapping("/add/save")
public String saveProject(@Valid @ModelAttribute("project") Project project,
BindingResult bindingResult,
Model model,
RedirectAttributes redirectAttributes) {
// bindingResult has error, because Thymeleaf can't map from the input-field to startDate
if (!bindingResult.hasErrors()) {
project.save(project);
return "redirect:/admin/projects/list";
} else {
return "admin/projects/add";
}
}
异常
Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDate' for property 'startDate'; nested
exception is
org.springframework.core.convert.ConversionFailedException: Failed to
convert from type [java.lang.String] to type
[@javax.persistence.Column java.time.LocalDate] for value
'2017-09-08'; nested exception is java.lang.IllegalArgumentException:
Parse attempt failed for value [2017-09-08]
您只能绑定可以用简单类型定义的模型对象。当对象从客户端序列化到服务器时,它不知道复杂类型(like java.time.LocalDate
),除非它们是用简单类型表示的。对于您的 scnerio,最好的方法是将客户端 HTML 与服务器端 java 对象日期映射为字符串。然后,您可以在服务器端控制器 class 或任何 java 服务 class.
中将字符串对象转换为 java.time.LocalDate
您有几个选择:
1 - 尝试:
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate startDate;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate endDate;
向 LocalDate 变量添加 @DateTimeFormat(pattern = "yyyy-MM-dd")
注释解决了问题。
我有一个输入字段(类型:'date')- 谁可以使用 Thymeleaf 将它映射到我的对象中的 'LocalDate' 字段?
对象
public class Project {
@Id
private int id;
private LocalDate startDate;
private LocalDate endDate;
}
HTML 输入
<form action="#"
th:action="@{|/admin/projects/add/save|}"
th:object="${newProjects}"
method="POST"
class="form-horizontal">
<input type="date" class="form-control" id="startDate"
placeholder="Project start"
th:field="*{startDate}"/>
<input type="date" class="form-control" id="endDate"
placeholder="Project start"
th:field="*{endDate}"/>
</form>
如何将输入字段正确映射到 LocalDate startDate 或 endDate?
控制器
//GetMapping for Projects is also there, but I didn't paste it to keep clarity
@PostMapping("/add/save")
public String saveProject(@Valid @ModelAttribute("project") Project project,
BindingResult bindingResult,
Model model,
RedirectAttributes redirectAttributes) {
// bindingResult has error, because Thymeleaf can't map from the input-field to startDate
if (!bindingResult.hasErrors()) {
project.save(project);
return "redirect:/admin/projects/list";
} else {
return "admin/projects/add";
}
}
异常
Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDate' for property 'startDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.persistence.Column java.time.LocalDate] for value '2017-09-08'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2017-09-08]
您只能绑定可以用简单类型定义的模型对象。当对象从客户端序列化到服务器时,它不知道复杂类型(like java.time.LocalDate
),除非它们是用简单类型表示的。对于您的 scnerio,最好的方法是将客户端 HTML 与服务器端 java 对象日期映射为字符串。然后,您可以在服务器端控制器 class 或任何 java 服务 class.
java.time.LocalDate
您有几个选择:
1 - 尝试:
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate startDate;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate endDate;
向 LocalDate 变量添加 @DateTimeFormat(pattern = "yyyy-MM-dd")
注释解决了问题。