文章來源:http://blog.csdn.net/kvgnt/archive/2011/02/21/6198750.aspx
struts2裡關於 Spring Aop 切面方法裡直接使用 com.opensymphony.xwork2.ActionContext 就可以得到了.初學繞了一個大彎..唉.
當然,action裡也可以這麼拿.不過action裡更建議實現 SessionAware, ServletRequestAware, ServletResponseAware 這3個介面來獲得.假如使用的是實現介面的方式,不要在建構函式裡使用這3個對象,因為action對象構建時這3個對象還不一定擷取到,可能null 指標.
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);
或直接用
HttpServletRequest request = ServletActionContext.getRequest();
下面這個過時了..貌似 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的最前面.保證一開始就得到變數
view plain copy to clipboard print ? <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模式的體會