zoukankan      html  css  js  c++  java
  • 登录保存用户信息

    做项目时,可能会将某些信息保存在session中,如登录等信息,这样方便在某些页面使用这些保存的信息。

    要想保存这些信息,需要创建一个类,该类里面定义需要保存的变量等信息,当登录后就通过new一个该类来保存登录等信息,然后放在session中,需要用到这些信息时直接用例如EL表达式等取出来就OK了。例子如下:

    1.保存用户信息的类

    1. <span style="font-size:14px;"><span style="font-family:Microsoft YaHei;">public class WSessionInfo implements java.io.Serializable {<span style="white-space:pre">    </span>  
    2.   
    3.     private String id;// 用户ID  
    4.     private String loginname;// 登录名  
    5.     private String name;// 姓名  
    6.     private String ip;// 用户IP  
    7.     private String userimg; //图片  
    8.     private RegisterUser user;  //用户注册信息      
    9.     private String usertype;//用户类型  
    10.     private String chengsid;//城市编号  
    11.     private String cityname;//城市名  
    12.     private List<String> resourceList;// 用户可以访问的资源地址列表  
    13.     private List<String> resourceAllList;  
    14.       
    15.     public String getId() {  
    16.         return id;  
    17.     }  
    18.   
    19.     public void setId(String id) {  
    20.         this.id = id;  
    21.     }  
    22.     public String getChengsid() {  
    23.         return chengsid;  
    24.     }  
    25.   
    26.     public void setChengsid(String chengsid) {  
    27.         this.chengsid = chengsid;  
    28.     }  
    29. <span style="white-space:pre">    </span>......  
    30. }</span>  
    31.   
    32. </span>  
    <span style="font-size:14px;"><span style="font-family:Microsoft YaHei;">public class WSessionInfo implements java.io.Serializable {<span style="white-space:pre"> </span>
    
        private String id;// 用户ID
        private String loginname;// 登录名
        private String name;// 姓名
        private String ip;// 用户IP
        private String userimg; //图片
        private RegisterUser user;  //用户注册信息    
        private String usertype;//用户类型
        private String chengsid;//城市编号
        private String cityname;//城市名
        private List<String> resourceList;// 用户可以访问的资源地址列表
        private List<String> resourceAllList;
        
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
        public String getChengsid() {
            return chengsid;
        }
    
        public void setChengsid(String chengsid) {
            this.chengsid = chengsid;
        }
    <span style="white-space:pre">    </span>......
    }</span>
    
    </span>

    2.在Controller中保存登录等信息

    1. <span style="font-size:14px;"><span style="font-family:Microsoft YaHei;"><span style="white-space:pre">   </span>@RequestMapping("/login")  
    2.     @ResponseBody  
    3.     public Json login(Account user, HttpSession session) {  
    4.         ......  
    5.         WSessionInfo sessionInfo = new WSessionInfo();//保存用户信息的类  
    6.         sessionInfo.setId(loginUser.getId());//为类里面的属性赋值,即把登录信息保存在这个类中  
    7.         sessionInfo.setLoginname(loginUser.getLoginname());  
    8.         sessionInfo.setUsertype(loginUser.getUsertype());  
    9.         sessionInfo.setUser(regUser);  
    10.         session.setAttribute(GlobalConstant.SESSION_WAP_INFO, sessionInfo);//将信息保存在session中   
    11.     }  
    12. </span></span>  
    <span style="font-size:14px;"><span style="font-family:Microsoft YaHei;"><span style="white-space:pre">  </span>@RequestMapping("/login")
        @ResponseBody
        public Json login(Account user, HttpSession session) {
            ......
            WSessionInfo sessionInfo = new WSessionInfo();//保存用户信息的类
            sessionInfo.setId(loginUser.getId());//为类里面的属性赋值,即把登录信息保存在这个类中
            sessionInfo.setLoginname(loginUser.getLoginname());
            sessionInfo.setUsertype(loginUser.getUsertype());
            sessionInfo.setUser(regUser);
            session.setAttribute(GlobalConstant.SESSION_WAP_INFO, sessionInfo);//将信息保存在session中 
        }
    </span></span>

    3.在页面使用

    1. <span style="font-size:14px;"><span style="font-family:Microsoft YaHei;"><%@page import="com.wxm.framework.constant.GlobalConstant"%>  
    2. <%@page import="com.wxm.pageModel.base.WSessionInfo"%>  
    3. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
    4. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
    5. <c:set var="ctx" value="${pageContext.request.contextPath}"/>  
    6. <%  
    7. String path = request.getContextPath();  
    8. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
    9.   
    10. <span style="white-space:pre">    </span><strong>WSessionInfo sessionInfo = (WSessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_WAP_INFO);//获取保存的登录等信息</strong>  
    11.                   
    12.     if(sessionInfo!=null){//判断保存的信息是否为空,即判断用户是否登录  
    13.         response.sendRedirect("/bd/wap/ar.jsp");//如果用户登录后就跳转到某个页面  
    14. }  
    15. %>  
    16.   
    17. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    18. <html xmlns="http://www.w3.org/1999/xhtml">  
    19. <head>  
    20. <base href="<%=basePath%>">  
    21. <meta http-equiv="content-type" content="text/html; charset=UTF-8">  
    22. <meta charset="utf-8">  
    23. <title>登录</title>  
    24. <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">  
    25. <meta name="apple-mobile-web-app-capable" content="yes">  
    26. <meta name="apple-mobile-web-app-status-bar-style" content="black">  
    27. </head>  
    28.   
    29. <body>  
    30. </body>  
    31. </html>  
    32. </span></span>  
    <span style="font-size:14px;"><span style="font-family:Microsoft YaHei;"><%@page import="com.wxm.framework.constant.GlobalConstant"%>
    <%@page import="com.wxm.pageModel.base.WSessionInfo"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <c:set var="ctx" value="${pageContext.request.contextPath}"/>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    
    <span style="white-space:pre">    </span><strong>WSessionInfo sessionInfo = (WSessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_WAP_INFO);//获取保存的登录等信息</strong>
                    
        if(sessionInfo!=null){//判断保存的信息是否为空,即判断用户是否登录
            response.sendRedirect("/bd/wap/ar.jsp");//如果用户登录后就跳转到某个页面
    }
    %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <base href="<%=basePath%>">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <title>登录</title>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    </head>
    
    <body>
    </body>
    </html>
    </span></span>

    4.退出session

    1. <span style="font-family:Microsoft YaHei;"><span style="white-space:pre">   </span>@RequestMapping("/logout")  
    2.     @ResponseBody  
    3.     public Json logout(HttpSession session) {  
    4. <span style="white-space:pre">        </span>Json j = new Json();//这个类是保存成功或失败的信息  
    5.         if (session != null) {  
    6.             session.invalidate();//调用session的invalidate()方法,将保存的session删除  
    7.         }  
    8.         j.setSuccess(true);  
    9.         j.setMsg("退出登录成功!");  
    10.         return j;  
    11. <span style="white-space:pre">    </span>}</span>  
    <span style="font-family:Microsoft YaHei;"><span style="white-space:pre">   </span>@RequestMapping("/logout")
        @ResponseBody
        public Json logout(HttpSession session) {
    <span style="white-space:pre">        </span>Json j = new Json();//这个类是保存成功或失败的信息
            if (session != null) {
                session.invalidate();//调用session的invalidate()方法,将保存的session删除
            }
            j.setSuccess(true);
            j.setMsg("退出登录成功!");
            return j;
    <span style="white-space:pre">    </span>}</span>
    

     

  • 相关阅读:
    Calling a parent window function from an iframe
    JSON with Java
    Posting array of JSON objects to MVC3 action method via jQuery ajax
    What's the difference between jquery.js and jquery.min.js?
    jquery loop on Json data using $.each
    jquery ui tabs详解(中文)
    DataTables warning requested unknown parameter
    Datatables 1.10.x在命名上与1.9.x
    jQuery 1.x and 2.x , which is better?
    DataTabless Add rows
  • 原文地址:https://www.cnblogs.com/qiu18359243869/p/12178683.html
Copyright © 2011-2022 走看看