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

    Session简单介绍

      在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下)。因此,在需要保存用户数据时,服务器程序可以把用户数据写到用户浏览器独占的session中,当用户使用浏览器访问其它程序时,其它程序可以从用户的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>



  • 相关阅读:
    安卓手机抓包
    探讨 yum 与 rpm 的安装包数量
    CentOS6.5 yum安装桌面环境
    CentOS6.5使用本地光盘做yum源 (参考:http://www.jb51.net/os/RedHat/43343.html)
    tar、zip 、unzip 打包与压缩 (参考:http://pengyl.blog.51cto.com/5591604/1191197)
    CentOS6.5使用本地光盘做yum源 (参考:http://www.jb51.net/os/RedHat/43343.html)
    mount 挂载光盘
    Shell编程
    Vim实现批量注释的方法
    Linux基本命令
  • 原文地址:https://www.cnblogs.com/ln0808/p/7063150.html
Copyright © 2011-2022 走看看