zoukankan      html  css  js  c++  java
  • spring MVC 如何获取session并实现传值到前台

    后台获取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(); 
    } 
  • 相关阅读:
    iOS: 学习笔记, Swift与C指针交互(译)
    kubernetes多节点部署的决心
    vim温馨提示
    简单工厂
    C++调用一个成员函数的需求this指针的情况
    hdoj 1226 超级password 【隐图BFS】
    Oracle Global Finanicals Technical Reference(一个)
    连载:面向对象的葵花宝典:思维、技能与实践(40)
    Android启动第三方应用程序
    BZOJ 1004 HNOI2008 Cards Burnside引理
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5146584.html
Copyright © 2011-2022 走看看