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