struts2拦截器的使用方式
一.struts2拦截器的使用方式1(实现Interceptor接口)
1.MyInterceptor.java(带参数的拦截器的定义)
package com.hitsoft.interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;import com.opensymphony.xwork2.interceptor.PreResultListener;@SuppressWarnings("serial")public class MyInterceptor implements Interceptor{private String hello;public String getHello() {return hello;}public void setHello(String hello) {this.hello = hello;}public void destroy() {System.out.println("destroy invoked!");}public void init() {System.out.println("init invoked!");System.out.println("hello=" + hello);}public String intercept(ActionInvocation invocation) throws Exception {invocation.addPreResultListener(new PreResultListener(){public void beforeResult(ActionInvocation invocation,String resultCode) {System.out.println("innerClass PreResultListener invoked!");}});System.out.println("MyInterceptor1 invoked!");String result= invocation.invoke();System.out.println("hello1=" + hello);System.out.println("result1="+result);return result;}}2.struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="struts2" extends="struts-default"><interceptors> <!-- 第一种拦截器 --> <interceptor name="myInterceptor" name="code">package com.hitsoft.interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;@SuppressWarnings("serial")public class MyInterceptor2 extends AbstractInterceptor{private String hello;public String getHello() {return hello;}public void setHello(String hello) {this.hello = hello;}@Overridepublic String intercept(ActionInvocation invocation) throws Exception {System.out.println("MyInterceptor2 invoked!");String result= invocation.invoke();System.out.println("hello2=" + hello);System.out.println("result2="+result);return result;}}2.struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="struts2" extends="struts-default"><interceptors> <!-- 第二种拦截器 --> <interceptor name="myInterceptor2" name="code">package com.hitsoft.interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;@SuppressWarnings("serial")public class MyInterceptor3 extends MethodFilterInterceptor{@Overrideprotected String doIntercept(ActionInvocation invocation) throws Exception {System.out.println("MyInterceptor3 invoked!");String result= invocation.invoke();System.out.println("result3="+result);return result;}}2.struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="struts2" extends="struts-default"><interceptors> <!-- 第三种拦截器 --> <interceptor name="myInterceptor3" name="code"><?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="struts2" extends="struts-default"><interceptors> <!-- 第一种拦截器 --> <interceptor name="myInterceptor" ></interceptor-ref> <interceptor-ref name="myInterceptor2"></interceptor-ref> <interceptor-ref name="myInterceptor3"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors><!-- 默认定义拦截器栈 --><default-interceptor-ref name="myStack"></default-interceptor-ref><action name="login" name="code"><?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="struts2" extends="struts-default"><interceptors> <!-- 第一种拦截器 --> <interceptor name="myInterceptor" ></interceptor-ref><interceptor-ref name="myInterceptor2"></interceptor-ref><interceptor-ref name="myInterceptor3"><param name="excludeMethods">execute</param><param name="includeMethods">execute</param></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref></action> </package></struts>