AdminDto.java:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class AdminDto {
@Size(min = 3, max = 10, message = "Invalid first name (3-10 characters allowed")
private String firstName;
@Size(min = 3, max = 10, message = "Invalid last name (3-10 characters allowed")
private String lastName;
private String username;
@Size(min = 5, max = 15, message = "Invalid password (5-10 characters allowed")
private String password;
private String repeatPassword;
}
LoginController.java:
@Controller
public class LoginController {
@Autowired
private AdminService adminService;
@GetMapping("/login")
public String loginForm() {
return "login";
}
@GetMapping("/register")
public String register(Model model) {
model.addAttribute("adminDto", new AdminDto());
return "register";
}
@GetMapping("/forgot-password")
public String forgotPassword(Model model) {
return "forgot-password";
}
@PostMapping("/register-new")
public String addNewAdmin(
@Valid @ModelAttribute("adminDto") AdminDto adminDto,
BindingResult result,
Model model,
RedirectAttributes redirectAttributes) {
// dto is not changed anywhere so could be just passed as is
model.addAttribute("adminDto", adminDto);
try {
// 1. errors in validation
if (result.hasErrors()) {
System.out.println("there were errors");
return "redirect:/register";
}
// 2. user is already registered
if (adminService.findByUsername(adminDto.getUsername()) != null) {
redirectAttributes.addFlashAttribute("message", "You are already registered");
return "redirect:/register";
}
// 3. password and repeatPassword do not equal
if (!adminDto.getPassword().equals(adminDto.getRepeatPassword())) {
redirectAttributes.addFlashAttribute("message", "Passwords are not same");
return "redirect:/register";
}
// 4. success
adminService.save(adminDto);
redirectAttributes.addFlashAttribute("message", "Registered successfully");
} catch (Exception e) {
// 5. all other errors
redirectAttributes.addFlashAttribute("message", "Unknown server error");
return "redirect:/register";
}
return "redirect:/register";
}
}
注册.html:
...
<form class="user" th:action="@{/register-new}" th:object="${adminDto}" method="post">
<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<input type="text" class="form-control form-control-user" id="exampleFirstName"
placeholder="First Name" th:field="*{firstName}" />
<div th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}" class="alert alert-danger">
</div>
</div>
<div class="col-sm-6">
<input type="text" class="form-control form-control-user" id="exampleLastName"
placeholder="Last Name" th:field="*{lastName}">
<div th:if="${#fields.hasErrors('lastName')}" th:errors="*{lastName}" class="alert alert-danger">
</div>
</div>
</div>
<div class="form-group">
<input type="email" class="form-control form-control-user" id="exampleInputEmail"
placeholder="Username" th:field="*{username}">
</div>
<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<input type="password" class="form-control form-control-user" id="exampleInputPassword"
placeholder="Password" th:field="*{password}">
<div th:if="${#fields.hasErrors('password')}" th:errors="*{password}" class="alert alert-danger">
</div>
</div>
<div class="col-sm-6">
<input type="password" class="form-control form-control-user" id="exampleRepeatPassword"
placeholder="Repeat Password" th:field="*{repeatPassword}">
</div>
</div>
<button class="btn btn-primary btn-user btn-block">
Register Account
</button>
</form>
...
AdminDto
中有 3 个字段具有 @Size
验证集(名字、姓氏和密码)。在register.html
形式中,都有
<div th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}" class="alert alert-danger"></div>
分别是他们的名字。但是,当输入错误的大小时(例如,在表单中,我只是为名字和姓氏输入一个字符),不会显示任何警报(但在控制台中,它将消息打印为
// 1 errors in validation
if (result.hasErrors()) {
System.out.println("there were errors");
return "redirect:/register";
}
那么为什么没有显示警报呢?是因为重定向吗?
是的,这是因为重定向。重定向所做的就是执行
public String register(Model model)
中的逻辑。除非使用 flash 属性,否则重定向时您会丢失状态。出现错误时通常遵循的模式是 return "register;"
(在您的情况下),它会呈现带有错误的页面,而不是在 /register 上执行重定向和 GET。