zoukankan      html  css  js  c++  java
  • 从session中获取当前用户的工具类

    package cn.crmx.crm.util;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    import cn.crmx.crm.domain.Employee;
    /**
     * @Name UserContextUtil
     * @Descr 用户上下文对象:设置和获取HttpSession登录的用户
     * @author lne
     * @date 2016年10月16日下午1:04:21
     */
    public class UserContextUtil {
        public static final String LOGIN_USER = "loginUser";
    
        public static void setUser(Employee emp, HttpSession session) {
            session.setAttribute(LOGIN_USER, emp);
        }
    
        public static Employee getUser(HttpSession session) {
            return (Employee)session.getAttribute(LOGIN_USER);
        }
    
        public static Employee getUser() {
            return (Employee)getsession().getAttribute(LOGIN_USER);
        }
    
        public static HttpSession getsession() {
            return getRequest().getSession();
        }
    
        public static HttpServletRequest getRequest()
        {
          ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
    
          HttpServletRequest request = requestAttributes.getRequest();
    
          return request;
        }
    }
    

    使用事项:
      1. 需要spring的contextjar包支持.
      2. 使用get方法即可获取相应的东西.

  • 相关阅读:
    2014年7月 记事
    从客户端中检测到有潜在危险的Request.Form值 的解决方法
    jquery parent() parents() closest()区别
    不包含适合于入口点的静态"Main"方法
    JQuery移除事件
    jQ的toggle()方法示例
    codeforces hello2018 D Too Easy Problems
    HDU-6084 寻找母串
    51Nod 1109 01组成N的倍数
    可重排列
  • 原文地址:https://www.cnblogs.com/jpfss/p/9081683.html
Copyright © 2011-2022 走看看