我为Spring Boot应用程序创建了一个带有<input type="date" ...>
的简单表单。我希望在控制器中将其转换为LocalDate
。我收到错误消息:
Fri Aug 17 15:32:01 CEST 2018
There was an unexpected error (type=Internal Server Error, status=500).
No primary or default constructor found for class java.time.LocalDate
这当然是正确的,但是-就我所知-没关系,因为根据文档:“ [...] @DateTimeFormat
批注应与Java上的java.time
一起使用8和春季4。”
控制器方法:
@PostMapping("/users")
public String register(String user, String pw, @DateTimeFormat(iso=DateTimeFormat.ISO.DATE) LocalDate beginning){
MyUser myUser = MyUser.create(user,pw);
MyUser.setEnabled(beginning);
repo.register(myUser);
return "redirect:/users/"+user;
}
html / thymeleaf:
<form method="POST">
<div><label> Benutzername : <input type="text" name="user"/> </label></div>
<div><label> Passwort: <input type="password" name="password"/> </label></div>
<div><label> Freigeschaltet ab (optional): <input type="date" name="beginning"/> </label></div>
<div><input type="submit" value="Registrieren"/></div>
</form>
pom(节选)
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
...
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
编辑:完整的html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Users</title>
</head>
<body>
<div th:insert="fragments :: logoutButton">...</div>
<div class ="user_list">
<h2>Registrierte Nutzer</h2>
<div>
<table border="1">
<tr>
<td>Name</td>
<td>Freigeschaltet ab</td>
<td>Freigeschaltet bis</td>
<td>Passwort gesetzt</td>
<td>Freigaben</td>
</tr>
<tr th:each="user : ${users}">
<td th:text="${user.userName}"/>
<td th:text="${user.enabled}"/>
<td th:text="${user.disabled}"/>
<td th:text="${user.pwSet}"/>
<td th:text="${user.authorities}"/>
</tr>
</table>
</div>
</div>
<div class ="register_user">
<h2>Neuen Benutzer Registrieren</h2>
<form method="POST">
<div><label> Benutzername : <input type="text" name="user"/> </label> </div>
<div><label> Password: <input type="password" name="password"/> </label></div>
<div><label> Freigeschaltet ab (optional): <input type="date" name="beginning"/> </label></div>
<div><input type="submit" value="Registrieren"/></div>
</form>
</div>
</body>
</html>
答案 0 :(得分:1)
LocaDate
不提供自己的构造函数,而是使用静态方法调用实例化的。
此问题已在SPR-13362中解决,并且可以正常工作。解决方法是使用@ModelAttribute
批注创建模型属性,并将其传递给请求:
@ModelAttribute
LocalDate initLocalDate() {
return LocalDate.now();
}
然后将其传递给请求:
@PostMapping("/users")
public String register(
String user,
String pw,
@DateTimeFormat(iso=DateTimeFormat.ISO.DATE) @ModelAttribute LocalDate beginning)
{
// ...
}