非action层 通过 ThreadLocal 获取 request值
来源:互联网 发布:淘宝官方旗舰店怎么开 编辑:程序博客网 时间:2024/06/07 10:17
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();
}
编写一个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()
- 非action层 通过 ThreadLocal 获取 request值
- Struts非Action层获取request
- jsp页面通过 request对象直接获取 struts2 Action的变量的值。
- struts2 action获取request
- action中获取request
- 通过action 使用 request.getParameter
- Struts2 Action中获取Request
- Struts Action中获取Request
- action中获取request对象
- Action中获取session,request
- action获取request和session
- struts2 Action中获取session,request,respon值
- struts2 Action中获取session,request,respon值
- java 页面上有ENCTYPE="multipart/form-data" 时action方法或者控制层用request.getParameter()获取不到值的方法
- Service层获取HttpServletRequest request
- 通过request.getContextPath获取绝对路径
- 通过ServletRequestAware接口获取request
- 通过request获取各种路径
- 思科VSS虚拟交换系统配置命令
- 点关于直线的距离、垂足、对称点公式
- MySQL别名的使用
- C++文件流ios::binary的作用
- pcduino安装rzsz
- 非action层 通过 ThreadLocal 获取 request值
- 应用内购买 RMStore
- CSS Hack (各个浏览器兼容的问题)
- Oracle 取随机数
- tomcat内存配置
- android开发之Notification_通知栏消息
- HashSet的底层实现了解和使用方法
- setTimer和Killtimer定时器的应用
- javaScript学习一