非action层 通过 ThreadLocal 获取 request值

通过ThreadLocal传递action层中的信息,如request对象等,到飞action层中进行引用。

public class SessionThreadLocal {
@SuppressWarnings("unchecked")
static ThreadLocal sessionUser = new ThreadLocal();

@SuppressWarnings("unchecked")
public static void setSessionUser(ServiceSessionInfo cSession) {
sessionUser.set(cSession);
}

public static ServiceSessionInfo getSessionUser(){
return (ServiceSessionInfo )sessionUser.get();
}

public static String getSessionId(){
return getSessionUser().getSessionId();
}
public static String getId(){
return getSessionUser().getId();
}
}
ServiceSessionInfo是一个pojo,里面放置一些属性和气get和set方法。

编写一个Filter,通过Filter为ThreadLocal设置需要传递的值

主要是从filter中获取servlet对象,再获取session中的登陆信息



/**
 * 通过filter获取servlet相关信息
 * 然后设置到ThreadLocal中,方便飞action层的对象使用
 *
 */


public class SessionFilter implements Filter {

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws ServletException, IOException {
HttpServletRequest hrequest = (HttpServletRequest) request;
ServiceSessionInfo cs = (ServiceSessionInfo) hrequest.getSession(true).getAttribute("custom_report");
if(cs!=null){
SessionThreadLocal.setSessionUser(cs);
}
    chain.doFilter(request,response);
}
public void init(FilterConfig config)throws ServletException {
}
public void destroy() {}
}

第三步:web.xml中加入filter

第四部:飞action层进行引用。例如SessionThreadLocal.getId()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值