作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我创建了一个带有 <input type="date" ...>
的简单表单对于 Spring Boot 应用程序。我希望它变成一个 LocalDate
在 Controller 中。我收到错误:
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
一起使用”
Controller 方法:
@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/百里香叶:
<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)
{
// ...
}
关于java - Spring 启动 + LocalDate : "No primary or default constructor",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51896874/
我是一名优秀的程序员,十分优秀!