• 从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方法即可获取相应的东西.

  • 相关阅读:
    堆排序
    冒泡排序
    描述字符串写入文件
    Java Socket编程,读服务器几个字符,再写入本地显示。
    final,finally,finalize区别
    圆桌问题 (a)
    表达式
    计算系数
    乌龟棋
    显示图像(多源dfs)
  • 原文地址:https://www.cnblogs.com/jpfss/p/9081683.html
Copyright © 2020-2023  润新知