码迷,mamicode.com
首页 > 编程语言 > 详细

spring MVC 如何获取session并实现传值到前台

时间:2016-01-20 22:18:48      阅读:925      评论:0      收藏:0      [点我收藏+]

标签:

后台获取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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!