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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-18
  • 2021-06-05
  • 2021-09-20
  • 2021-07-01
  • 2021-11-05
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
相关资源
相似解决方案