我有一个登录表单,我想显示登录用户的名称。我想在Spring MVC中使用会话,但它不起作用
这是UsersController
@PostMapping("/room/saveRegister")
public String saveRegister(@Valid Users users, BindingResult result, RedirectAttributes redirect) {
if (result.hasErrors()) {
return "register";
}
usersService.save(users);
redirect.addFlashAttribute("success", "Saved user successfully!");
return "redirect:/room";
}
@GetMapping("/room/login")
public String login(Model model) {
model.addAttribute("users", new Users());
return "login";
}
@PostMapping("/room/loginRoom")
public String login(@ModelAttribute("users") Users user, ModelMap modelMap, RedirectAttributes redirect,HttpSession session) {
if(usersService.findByEmailAndPass(user.getEmail(),user.getPass()) != null) {
session.setAttribute("name",user.getName());
return "redirect:/room";
}else {
modelMap.put("error", "Email or Password is not correct. Pleased Try Again");
return "login" ;
}
}
在文件.html中,我添加了
<li ><a href="account.html"><span th:text ="${session.name}"></span></a></li>
但是当我登录成功时。不显示用户名。会话不起作用。我不知道为什么
答案 0 :(得分:0)
您需要使用${sessionScope.name}
代替${session.name}
答案 1 :(得分:0)