我在 UserController 里面写了几个处理用户注册、登录的方法,主要使用 @ModelAttribute 与 @ SessionAttributes 这两个注解进行数据绑定。启动项目,却一直报 org.springframework.web.HttpSessionRequiredException: Expected session attribute 'user' 的异常,是我对这两个注解的使用有问题吗?代码如下:@Controller@SessionAttributes("user")public class UserController { private final UserService userService; @Autowired public UserController(UserService userService) { this.userService = userService; } @RequestMapping(value = "/register", method = RequestMethod.GET) public String register(Model model) { User user = new User(); model.addAttribute("user", user); return "register"; } @RequestMapping(value = "/register", method = RequestMethod.POST) public String register(@ModelAttribute("user") User user, Model model) { User _user = userService.getUserByUsername(user.getUsername()); if (_user != null) { model.addAttribute("message", "用户已存在,请使用其他账号注册"); return "register"; } else { userService.insertUser(user); return "redirect:login"; } }Spring 版本:4.1.6.RELEASE服务器 JDK 版本:1.6.0_26服务器 Tomcat 版本:7.0.42求大神帮忙解决,不胜感激!
添加回答
举报
0/150
提交
取消