第一種方法:
直接implements實(shí)現(xiàn)com.opensymphony.xwork2.interceptor.Interceptor
- public class MyInterceptor implements Interceptor {
-
- public void destroy() {
- System.out.println("destroy()");
- }
-
- public void init() {
- System.out.println("init()");
- }
-
- public String intercept(ActionInvocation invocation) throws Exception {
- System.out.println("Interceptor");
- String result = invocation.invoke();
- return result;
- }
-
- }
第二種方法
直接extends com.opensymphony.xwork2.interceptor.AbstractInterceptor
這種方法是少了destroy,init方法
- public class MyInterceptor2 extends AbstractInterceptor {
-
- @Override
- public String intercept(ActionInvocation invocation) throws Exception {
- System.out.println("MyInterceptor2222");
- String result = invocation.invoke();
- return result;
- }
-
- }
第三種方法
直接extends com.opensymphony.xwork2.interceptor.MethodFilterInterceptor
這種方法能對(duì)Action里面的方法級(jí)進(jìn)行控制,是否執(zhí)行這個(gè)方法
- public class MyInterceptor3 extends MethodFilterInterceptor {
-
- @Override
- protected String doIntercept(ActionInvocation invocation) throws Exception {
- System.out.println("use MethodFilterInterceptor");
- String result = invocation.invoke();
- return result;
- }
-
- }
然就是配置
- <interceptors>
- <interceptor name="myInterceptor" class="com.langhua.interceptor.MyInterceptor">
- </interceptor>
-
- <interceptor name="myInterceptor2" class="com.langhua.interceptor.MyInterceptor2">
- </interceptor>
-
- <interceptor name="myInterceptor3" class="com.langhua.interceptor.MyInterceptor3">
- <param name="excludeMethods">execute</param>
- <param name="includeMethods">addmessage</param>
- </interceptor>
-
- <interceptor-stack name="myInterceptorStack">
- <interceptor-ref name="myInterceptor3"></interceptor-ref>
- <interceptor-ref name="defaultStack"></interceptor-ref>
- </interceptor-stack>
- </interceptors>
里面分interceptor和interceptor-stack
stack也能引用stack
default-interceptor-ref表示Action如果不指定interceptor就用這個(gè)default的
extends="struts-default"表示這個(gè)XML文件是extends 另一個(gè)xml的,名字叫struts-default
在這個(gè)XML中,配置了一個(gè)常用的interceptor可以去Core包中找到這個(gè)XML
interceptor配置很靈活的。如果要寫(xiě)自己的interceptor就要把<interceptor-ref name="defaultStack"></interceptor-ref>默認(rèn)的給加上,當(dāng)然也可以自己配置。
interceptor的參數(shù)
- <interceptor-stack name="myInterceptorStack">
- <interceptor-ref name="myInterceptor3"></interceptor-ref>
- <interceptor-ref name="defaultStack"></interceptor-ref>
- </interceptor-stack>
這個(gè)是因?yàn)镸ethodFilterInterceptor 里有這兩個(gè)成員變量,如果你自己的interceptor要帶參數(shù)的話就要相應(yīng)的Action里面寫(xiě)到成員變量,并加上get,set方法
- public abstract class MethodFilterInterceptor extends AbstractInterceptor {
- protected transient Logger log = LoggerFactory.getLogger(getClass());
-
- protected Set<String> excludeMethods = Collections.emptySet();
- protected Set<String> includeMethods = Collections.emptySet();
監(jiān)聽(tīng)器
首先要實(shí)現(xiàn)com.opensymphony.xwork2.interceptor.PreResultListener類
并重寫(xiě)里面的方法beforeResult
- public class MyListener implements PreResultListener {
-
- public void beforeResult(ActionInvocation invocation, String resultCode) {
- System.out.println(resultCode);
- }
-
- }
然后再在攔截器里面調(diào)用
- invocation.addPreResultListener(new MyListener());
監(jiān)聽(tīng)器是在這個(gè)攔截器完成別的攔截器之后調(diào)用的
struts2 Action獲得HttpSession,HttpServletRequest,HttpSevletResponse的方法
非IOC方式
這種方式主要是利用了com.opensymphony.xwork2.ActionContext類以及org.apache.struts2.ServletActionContext類
- ActionContext ctx = ActionContext.getContext();
-
- HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
-
- HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);
-
-
-
-
-
- HttpServletRequest request = ServletActionContext.getRequest ();
主要是這兩個(gè)類com.opensymphony.xwork2.ActionContext和org.apache.struts2.ServletActionContext都對(duì)request等進(jìn)行了大量的封裝,直接調(diào)用方法就可以獲和
更好一點(diǎn)的IOC方式
action類實(shí)現(xiàn)ServletRequestAware接口,并新建一個(gè)HttpServletRequest request
- public class UserLoginAction extends ActionSupport implements ServletRequestAware{
- public void setServletRequest(HttpServletRequest request) {
- this.request=request;
- }
- 然后可以生成的request得到對(duì)象,如request.getRemoteAddr()
- action類實(shí)現(xiàn)SessionAware接口,并創(chuàng)建一個(gè)MAP對(duì)象session
- public class UserLoginAction extends ActionSupport implements ServletRequestAware,SessionAware{
- public void setServletRequest(HttpServletRequest request) {
- this.request=request;
- }
- public void setSession(Map session) {
- this.session=session;
}
這些獲得HttpServletRequest等對(duì)象需要implments的接口都在
org.apache.struts2.interceptor下面
如Apllication的是ApplicationAware
如HttpSession的是SessionAware(struts2的Session都被封裝成Map了)
如HttpServletRequest的是ServletRequestAware
如HttpServletResponse的是ServletResponseAware
|