Article Source: http://blog.csdn.net/kvgnt/archive/2011/02/21/6198750.aspx
Struts2 in the Spring Aop aspect method, the direct use of the com.opensymphony.xwork2.ActionContext can be obtained. Beginner around a big bend. Alas.
Of course, this can be done in action. However, the action is more recommended to implement Sessionaware, Servletrequestaware, servletresponseaware these 3 interfaces to obtain. If you are using a way to implement an interface, Do not use these 3 objects in constructors, because these 3 objects are not necessarily acquired when the action object is built, and may be null pointers.
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);
or directly with
HttpServletRequest request = Servletactioncontext.getrequest ();
The following is out of date: It looks like the struts1 is doing it.
create a class with 2 static variables threadlocal to hold the request and response of the current thread
PackageCom.exdoit.tool;ImportJavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportJavax.servlet.http.HttpSession; Publicclasssyscontent {PrivateStaticThreadlocalNewThreadlocalPrivateStaticThreadlocalNewThreadlocal PublicStaticHttpServletRequest Getrequest () {return(HttpServletRequest) Requestlocal.get (); } PublicStaticvoidSetrequest (HttpServletRequest request) {Requestlocal.set (request); } PublicStaticHttpServletResponse GetResponse () {return(HttpServletResponse) Responselocal.get (); } PublicStaticvoidSetresponse (httpservletresponse response) {Responselocal.set (response); } PublicStaticHttpSession getsession () {return(HttpSession) ((HttpServletRequest) Requestlocal.get ()). GetSession (); } }
Create a filter. assigns the current thread's request and response to the static variables of the previous class
PackageCOM.EXDOIT.AOP;ImportJava.io.IOException;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;ImportJavax.servlet.ServletException;ImportJavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse;ImportJavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportCom.exdoit.tool.SysContent; Public classGetContentImplementsFilter {@Override PublicvoidDestroy () {//TODO auto-generated method stub} @Override PublicvoidDoFilter (ServletRequest arg0, Servletresponse arg1, Filterchain arg2)throwsIOException, servletexception {syscontent.setrequest (httpservletrequest) arg0); Syscontent.setresponse ((HttpServletResponse) arg1); Arg2.dofilter (arg0, arg1); } @Override PublicvoidInit (Filterconfig arg0)throwsservletexception {//TODO auto-generated method stub}}
Configure Web. XML to load a filter I put this node at the very beginning of web. Guaranteed to get a variable from the start
View plain Copy to clipboard print? <filter> <filter-name> GetContent</filter-name> <filter-class>com.exdoit.aop.GetContent</filter-c lass> </filter> <filter-mapping> <filter-name>G Etcontent</filter-name> <url-pattern>*</url-pattern> </filter-mapping>
how to execute in interceptors
Public void rightfilter (joinpoint joinpoint) throws Exception {httpservletrequest request = Syscont Ent.getrequest (); HttpServletResponse response = Syscontent.getresponse (); HttpSession session = Syscontent.getsession (); Other actions}
After the other interceptor or action ... such as objects, use these 3 variables. Can be referenced directly through SYSCONTENT.GETXX () http://www.javaeye.com/topic/103804 Correct understanding threadlocal http:// www.javaeye.com/topic/617368 on the threadlocal model