后台获取session:
@RequestMapping("/usrlogin")public ModelAndView usrlogin(@RequestParam String usrid, @RequestParam String passwd) { HttpSession session = getSession(); User user = new User(); user = userMapper.getUserByUsridAndPasswd(usrid, passwd); if (null != user) { session.setAttribute("user", user.getUser_id());// User user1=(User)session.getAttribute("user");// System.out.println(user1.getUser_name()); return new ModelAndView("main"); } else { return new ModelAndView("error"); } } public static HttpSession getSession() { HttpSession session = null; try { session = getRequest().getSession(); } catch (Exception e) {} return session; } public static HttpServletRequest getRequest() { ServletRequestAttributes attrs =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); return attrs.getRequest(); }
前台jsp取值:
<% HttpSession s = request.getSession(); %><%=s.getAttribute("user")%>
@RequestMapping("/usrlogin")public ModelAndView usrlogin(@RequestParam String usrid,@RequestParam String passwd) {HttpSession session = getSession();User user = new User();user = userMapper.getUserByUsridAndPasswd(usrid, passwd);if (null != user) {session.setAttribute("user", user.getUser_id());// User user1=(User)session.getAttribute("user");// System.out.println(user1.getUser_name());return new ModelAndView("main");} else {return new ModelAndView("error");}} public static HttpSession getSession() { HttpSession session = null; try { session = getRequest().getSession(); } catch (Exception e) {} return session; } public static HttpServletRequest getRequest() { ServletRequestAttributes attrs =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); return attrs.getRequest(); }