【发布时间】:2023-03-27 21:11:01
【问题描述】:
当用户想在我的网站上注册时,我想验证电子邮件。我设置了@NotBlank 和@Email 注释,在post 方法中做了@Valid 注释,但我不知道如何让它正常工作。在 /register 端点应用程序显示 error500。如何更改我的代码以使其正常工作?用户名不应该用空白或错误的电子邮件注册,但我无法解决。
AppUser 类:
@Data
@Entity
public class AppUser implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true)
@NotBlank(message = "login is required")
private String username;
@NotBlank(message = "password is required")
private String password;
private String role;
@Column(unique = true)
@Email(message = "Wrong e-mail")
private String email;
private String phone;
public AppUser() {
}
用户控制器类:
@Controller
public class UserController {
@GetMapping("register")
public String register(Model model) {
model.addAttribute("user", new AppUser());
return "register";
}
@PostMapping("/registerOk")
public String registerOk(@Valid AppUser appUser, Errors errors) {
if (errors.hasErrors()) {
return "register";
}
userService.addUser(appUser);
return "redirect:register?success";
}
}
我的注册.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset="UTF-8">
<title>Rejestracja</title>
<link rel="stylesheet" type="text/css" th:href="@{/css/index.css}"/>
</head>
<body>
<form class="box" th:action="@{/registerOk}" method="post">
<div th:if="${param.success}">
<div class="alert alert-info"><span style="color: white">Register is sucessful, check your e-mail adress.</span></div>
</div>
<h1>Register new account: </h1>
<input type="text" name="username" placeholder="login">
<span class="validationError"
th:if="${#fields.hasErrors('username')}"
th:errors="*{username}">Username Error</span>
<input type="password" name="password" placeholder="password" id="hide">
<input type="text" name="email" placeholder="E-Mail">
<input type="text" name="phone" placeholder="phone number">
<input type="submit" value="Register me!">
<div class="form-group">
<span style="color: white">Already registered? <a href="/" th:href="@{/login}">Login</a></span>
</div>
</form>
</body>
</html>
【问题讨论】:
-
500 是内部服务器错误。查看您的服务器日志文件以获取更多信息
-
我得到了一条信息:原因:org.attoparser.ParseException:评估 SpringEL 表达式的异常:“#fields.hasErrors('username')”在 IntelliJ 说无法解析位于我的 AppUser 类。
-
您可以查看服务器日志文件并发布您在那里找到的堆栈跟踪吗?
-
pastebin.com/ye1ep1qq - 这是我在 IntelliJ 中得到的。 pastebin.com/jHgyPUHj - 这是我在 localhost:8080/register 得到的...我认为这是 用户名错误 。 Intellij 红色突出显示我的“*{username}”
-
java.lang.IllegalStateException:在 org.springframework.web.servlet.support.BindStatus.
( BindStatus.java:153) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE] at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) ~[spring -webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
标签: spring-boot thymeleaf