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(); 
    } 
  • 相关阅读:
    媒体查询
    H5 progress标记
    sqlite数据库常用语句
    C#导出Word文档开源组件DocX
    bat实现监控进程守护程序-保证平台服务的稳定执行
    windows开机自动执行bat脚本启动cmd命令窗口并执行命令,最后自动关闭cmd命令窗
    Python实现定时执行任务的三种方式简单示例
    bat实现守护程序保证平台正常运行
    Windows CMD命令大全
    Windows下使用批处理文件.bat删除旧文件
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5146584.html
Copyright © 2011-2022 走看看