标签:
后台获取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(); %> <span><%=s.getAttribute("user")%></span>
@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(); }
spring MVC 如何获取session并实现传值到前台
标签:
原文地址:http://www.cnblogs.com/zhujiabin/p/5146584.html