[英]Spring Boot + LocalDate: “No primary or default constructor”
我為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 8和Spring 4上的java.time
。”
控制器方法:
@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>
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)
{
// ...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.