它显示了我应该在其中注册但不能在数据库中的表中添加用户的页面。我有三个表:角色,用户,user_role。在user_role中,我保存表角色和用户的外键。编译时没有错误,但是不会将数据插入到用户表中。
这是用户控制器:
@Controller
public class UserController {
@Autowired
private UserService userService;
@Autowired
private SecurityService securityService;
@Autowired
private UserValidator userValidator;
@GetMapping("/registration")
public String registration(Model model) {
model.addAttribute("userForm", new User());
return "registration";
}
@GetMapping("/admin")
public String admin() {
return "admin";
}
@PostMapping("/registration")
public String registration(@ModelAttribute("userForm") User userForm, BindingResult bindingResult, Model model) {
userValidator.validate(userForm, bindingResult);
if (bindingResult.hasErrors()) {
return "registration";
}
userService.save(userForm);
securityService.autoLogin(userForm.getUsername(), userForm.getPasswordConfirm());
return "redirect:/login";
}
@GetMapping("/login")
public String login(Model model, String error, String logout) {
if (error != null)
model.addAttribute("error", "Your username and password is invalid.");
if (logout != null)
model.addAttribute("message", "You have been logged out successfully.");
return "login";
}
}
这是用于注册的html页面:
<form method="POST" th:action="@{/registration}" id="registerForm" th:object="${userForm}" >
<label>Username: </label>
<input type="text" name="username" th:field="*{username}" /><br/>
<span class="validationError"
th:if="${#fields.hasErrors('username')}"
th:errors="*{username}"
th:onmessage="rgrfefewfefef"
style="color:black;"> [error]</span>
<label>Password: </label>
<input type="password" name="password" th:field="*{password}" /><br/>
<span class="validationError"
th:if="${#fields.hasErrors('password')}"
th:errors="*{password}"
style="color:black;"> password Error</span>
<label>Confirm password: </label>
<input type="password" name="confirm" th:field="*{passwordConfirm}" /><br/>
<span class="validationError"
th:if="${#fields.hasErrors('passwordConfirm')}"
th:errors="*{passwordConfirm}"
style="color:black;"> passwordConfirm Error</span>
<input type="submit" value="Register"/>
</form>