Spring AOP 取得Struts的Session、Request、Response

40 篇文章 0 订阅

struts2里关于 Spring Aop 切面方法里直接使用 com.opensymphony.xwork2.ActionContext 就可以得到了.初学绕了一个大弯..唉.

当然,action里也可以这么拿.不过action里更建议实现 SessionAware, ServletRequestAware, ServletResponseAware 这3个接口来获得.假如使用的是实现接口的方式,不要在构造函数里使用这3个对象,因为action对象构建时这3个对象还不一定获取到,可能空指针.

Map<String,Object> session =  (Map<String,Object>)ActionContext.getContext().getSession();
HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);

String path = request.getRealPath("/"); 
Java显示已过时,替代方法为 request.getSession().getServletContext().getRealPath("/"); 

注: request.getRealPath("")
     就是取得你当前运行文件在服务器上的绝对路径.
     request.getRealPath("/")
     取得当前站点的根目录. 

 


下面这个过时了..貌似 struts1 里是这么干的.

 

 

建立一个类,里面的2个静态变量ThreadLocal用来保存当前线程的request和response

package com.exdoit.tool;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class SysContent
{
    private static ThreadLocal < HttpServletRequest > requestLocal = new
        ThreadLocal < HttpServletRequest > ();
    private static ThreadLocal < HttpServletResponse > responseLocal = new
        ThreadLocal < HttpServletResponse > ();
    public static HttpServletRequest getRequest()
    {
        return (HttpServletRequest)requestLocal.get();
    }
    public static void setRequest(HttpServletRequest request)
    {
        requestLocal.set(request);
    }
    public static HttpServletResponse getResponse()
    {
        return (HttpServletResponse)responseLocal.get();
    }
    public static void setResponse(HttpServletResponse response)
    {
        responseLocal.set(response);
    }
    public static HttpSession getSession()
    {
        return (HttpSession)((HttpServletRequest)requestLocal.get()).getSession
            ();
    }
}

创建一个过滤器.把当前线程的request和response赋到之前类的静态变量里

package com.exdoit.aop;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.exdoit.tool.SysContent;
public class GetContent implements Filter
{
    @Override
    public void destroy()
    {

        // TODO Auto-generated method stub}
        @Override
        public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2)throws IOException, ServletException
        {
            SysContent.setRequest((HttpServletRequest)arg0);
            SysContent.setResponse((HttpServletResponse)arg1);
            arg2.doFilter(arg0, arg1);
        }
        @Override
        public void init(FilterConfig arg0)throws ServletException
        {
            // TODO Auto-generated method stub}
        }
    }
}

配置WEB.XML,加载过滤器我把这段节点放到了web.xml的最前面.保证一开始就得到变量

<filter>
  <filter-name>GetContent</filter-name>
  <filter-class>com.exdoit.aop.GetContent</filter-class>
</filter>
<filter-mapping>
  <filter-name>GetContent</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

拦截器里的执行方法

public void rightFilter(JoinPoint joinPoint)throws Exception
{
    HttpServletRequest request = SysContent.getRequest();
    HttpServletResponse response = SysContent.getResponse();
    HttpSession session = SysContent.getSession();
    //其他操作
}

之后在其他拦截器或action...等对象中,要使用这3个变量.都可以直接通过SysContent.getXX() 得到

 

参考
http://www.javaeye.com/topic/103804  正确理解ThreadLocal
http://www.javaeye.com/topic/617368  关于ThreadLocal模式的体会

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值