我正在使用Spring Boot将使用jsp的Web应用程序转换为Thymeleaf。当用户提交表单以创建其帐户时,我的用户注册页面返回内部服务器错误状态= 500。当日志记录设置为debug时,以下是来自Console。
11:19:48.770 [http-nio-8080-exec-10] DEBUG o.s.b.w.f.OrderedRequestContextFilter#104 Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@61351829
11:19:48.772 [http-nio-8080-exec-10] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet]#181 Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at org.sub.account.service.SecurityServiceImpl.autologin(SecurityServiceImpl.java:38)
at org.sub.web.controller.WelcomeController.registration(WelcomeController.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
来自WelcomeController的第68行:
securityService.autologin(userForm.getUsername(), userForm.getPasswordConfirm());
来自SecurityServiceImpl的第38行:
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userDetails, password, userDetails.getAuthorities());
正如您在WelcomeController的此片段中所见,@ ModelAttribute被指定为“userForm”。
@RequestMapping(value = "/registration", method = RequestMethod.POST)
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:/welcome";
}
我的形式:对象具体说“userForm”
<form action="#" th:action="@{/registration.html}" th:object="${userForm}" method="POST">
我将我的th:fields设置为我们在jsp文件时的路径。
<input type="text" th:field="*{username}" class="form-control" placeholder="Username"></input>
...
<input type="password" th:field="*{password}" class="form-control" placeholder="Password"></input>
...
<input type="password" th:field="*{passwordConfirm}" class="form-control" placeholder="Confirm your password"></input>