暂时理解不够彻底 有空在详细介绍,先记录代码
1:创建一个工具类 存取当前登录用户
1 package com.liveyc.eloan.util; 2 3 import javax.servlet.http.HttpSession; 4 5 import org.springframework.web.context.request.RequestContextHolder; 6 import org.springframework.web.context.request.ServletRequestAttributes; 7 8 import com.liveyc.eloan.base.domain.Logininfo; 9 10 11 /**12 * 存取当前登录用户的工具类13 * 14 * @author Administrator15 * 16 */17 public class UserContext {18 19 public static final String CURRENT_LOGININFO_IN_SESSION = "logininfo";20 21 private static HttpSession getSession() {22 return ((ServletRequestAttributes) RequestContextHolder23 .getRequestAttributes()).getRequest().getSession();24 }25 26 public static void putCurrent(Logininfo current) {27 getSession().setAttribute(CURRENT_LOGININFO_IN_SESSION, current);28 }29 30 public static Logininfo getCurrent() {31 return (Logininfo) getSession().getAttribute(32 CURRENT_LOGININFO_IN_SESSION);33 }34 35 }
2:在Controller所在的项目的web.xml中配置监听器
12 org.springframework.web.context.request.RequestContextListener 3
看源码可发现 包装过程
所以在util工具类中 session的获取方式