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" class="com.hitsoft.interceptor.MyInterceptor">
    			<param name="hello">world</param>
    		</interceptor>
	</interceptors>
	<action name="login" class="com.hitsoft.action.LoginAction">
		<result name="success">/result.jsp</result>
		<result name="input">/login.jsp</result>
		<interceptor-ref name="myInterceptor"/>
	</action>
    </package>
</struts>

二.struts2拦截器的使用方式2(继承AbstractInterceptor抽象类)
1.MyInterceptor2.java(带参数的拦截器的定义)
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;
	}
	@Override
	public 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" class="com.hitsoft.interceptor.MyInterceptor2">
    			<param name="hello">world</param>
    		</interceptor>
	</interceptors>
	<action name="login" class="com.hitsoft.action.LoginAction">
		<result name="success">/result.jsp</result>
		<result name="input">/login.jsp</result>
		<interceptor-ref name="myInterceptor2"/>
	</action>
    </package>
</struts>


三.struts2拦截器的使用方式3(继承MethodFilterInterceptor抽象类)
1.MyInterceptor3.java(带参数的拦截器的定义)
package com.hitsoft.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

@SuppressWarnings("serial")
public class MyInterceptor3 extends MethodFilterInterceptor{

	@Override
	protected 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" class="com.hitsoft.interceptor.MyInterceptor3">
    			<param name="hello">world</param>
    		</interceptor>
	</interceptors>
	<action name="login" class="com.hitsoft.action.LoginAction">
		<result name="success">/result.jsp</result>
		<result name="input">/login.jsp</result>
		<interceptor-ref name="myInterceptor3"/>
	</action>
    </package>
</struts>


四.struts2拦截器的使用方式4(默认定义拦截器)
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" class="com.hitsoft.interceptor.MyInterceptor">
    			<param name="hello">world</param>
    		</interceptor>
    		<!-- 第二种拦截器 -->
    		<interceptor name="myInterceptor2" class="com.hitsoft.interceptor.MyInterceptor2">
    			<param name="hello">world</param>
    		</interceptor>
    		<!-- 第三种拦截器 -->
    		<interceptor name="myInterceptor3" class="com.hitsoft.interceptor.MyInterceptor3">
    		</interceptor>
    		<!-- 第四种拦截器栈 -->
    		<interceptor-stack name="myStack">
	    		<interceptor-ref 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" class="com.hitsoft.action.LoginAction">
		<result name="success">/result.jsp</result>
		<result name="input">/login.jsp</result>
	</action>
    </package>
</struts>

5.自定义并配置所有拦截器到action配置中:
<?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" class="com.hitsoft.interceptor.MyInterceptor">
    			<param name="hello">world</param>
    		</interceptor>
    		<!-- 第二种拦截器 -->
    		<interceptor name="myInterceptor2" class="com.hitsoft.interceptor.MyInterceptor2">
    			<param name="hello">world</param>
    		</interceptor>
    		<!-- 第三种拦截器 -->
    		<interceptor name="myInterceptor3" class="com.hitsoft.interceptor.MyInterceptor3">
    		</interceptor>
    	</interceptors>
	<action name="login" class="com.hitsoft.action.LoginAction">
		<result name="success">/result.jsp</result>
		<result name="input">/login.jsp</result>
		<interceptor-ref 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>

你可能感兴趣的:(struts2,Interceptor)