我想要捕获事件添加属性"fullName"
范围会话,我尝试使用ApplicationListener
它不起作用。
这是我的cotroller
@SessionAttributes({ "fullName" })
public class LoginController {
@RequestMapping(value = "home", method = RequestMethod.POST)
ModelAndView loginPost(ModelMap model, @ModelAttribute User user, HttpSession session) {
String fullName = loginService.getUserFullName(user);
if (fullName == null) {
model.addAttribute("error", "Username or password is invalid");
return new ModelAndView("login");
} else {
model.addAttribute("fullName", fullName);
return new ModelAndView("home", model);
}
}
和听众
public class Listener implements ApplicationListener<ApplicationEvent> {
private static int countUserOnline;
public static int getCountUserOnline() {
return countUserOnline;
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof HttpSessionDestroyedEvent) {
countUserOnline--;
}
else if(event instanceof HttpSessionDestroyedEvent) {
countUserOnline++;
}
}
}