在web.xml中配置
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>com.hrm.controller.SessionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>*.do</url-pattern>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
在controller 配置SessionFilter类
@Controller @RequestMapping("/user") public class UserController { @Autowired @Qualifier("UserService") private IUserService userService; @RequestMapping("/login") public String login(User user, HttpSession session, Model model){ System.out.println(user); User login_user = userService.findUserByUserNameAndPassWord(user); System.out.println(login_user); if(login_user != null){ session.setAttribute("login_user",login_user); return "/jsp/main.jsp"; }else{ model.addAttribute("message","账号或密码错误,请重新输入"); return "/index.jsp"; } } @RequestMapping("/logout") public String logout(HttpSession session){ session.removeAttribute("login_user"); return "redirect:/index.jsp"; } @RequestMapping public String findAllUser(Model model){ List<User> users =userService.findAllUser(); return "/jsp/user/user.jsp"; } }