1. 程式人生 > >Struts2的攔截器入門

Struts2的攔截器入門

1 攔截器與過濾器的區別?

  • Struts2的核心功能都是依賴攔截器實現
  • Filter:過濾器,過濾從客戶端向伺服器傳送的請求
  • Interceptor:攔截器,攔截是客戶端對Action的訪問。更細粒度化的攔截。(攔截Action中的具體的方法)。

2 Struts的攔截器入門

2.1 編寫攔截器類

  • 實現Interceptor介面

伺服器啟動的時候執行init方法,在String str = invocation.invoke()前的是前處理,在其後的是後處理

package com.itykd.web.interceptor;

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

public class MyInterceptor1 implements Interceptor{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("MyInterceptor1銷燬了.....");
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		System.out.println("MyInterceptor1初始化.....");
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("MyInterceptor1執行了....");
		String str = invocation.invoke();
		System.out.println("MyInterceptor1執行結束了....");
		return str;
	}
}
  • 繼承AbstractInterceptor類
package com.itykd.web.interceptor;

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

public class MyInterceptor2 extends AbstractInterceptor{

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println("MyInterceptor2執行了....");
		String str = invocation.invoke();
		System.out.println("MyInterceptor2執行結束了....");
		return str;
	}
}
  • 繼承MethodFilterInteceptor類
package com.itykd.web.interceptor;

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

public class MyInterceptor3 extends MethodFilterInterceptor{



	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		System.out.println("MyInterceptor3執行了....");
		String str = invocation.invoke();
		System.out.println("MyInterceptor3執行結束了....");
		return str;
	}
}

2.2 在struts.xml中配置攔截器

  • 定義攔截器
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
    <package name="demo1" extends="struts-default" namespace="/">
        <!-- 定義攔截器 -->
        <interceptors>
            <interceptor name="myInterceptor1" class="com.itykd.web.interceptor.MyInterceptor1"/>
            <interceptor name="myInterceptor2" class="com.itykd.web.interceptor.MyInterceptor2"/>
            <interceptor name="myInterceptor3" class="com.itykd.web.interceptor.MyInterceptor3"/>
        </interceptors>
        <action name="interceptorAction1" class="com.itykd.web.action.InterceptorAction1">
            <result name="success">/demo1.jsp</result>
            <!-- 引入攔截器棧 (一旦引入自定義攔截器,預設攔截器棧的攔截器就不執行了)-->
            <interceptor-ref name="defaultStack"/>
            <interceptor-ref name="myInterceptor1"/>
            <interceptor-ref name="myInterceptor2"/>
            <interceptor-ref name="myInterceptor3"/>
        </action>
    </package>
</struts>
  • 定義一個攔截器棧
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
    <package name="demo1" extends="struts-default" namespace="/">
        <!-- 定義攔截器 -->
        <interceptors>
            <interceptor name="myInterceptor1" class="com.itykd.web.interceptor.MyInterceptor1"/>
            <interceptor name="myInterceptor2" class="com.itykd.web.interceptor.MyInterceptor2"/>
            <interceptor name="myInterceptor3" class="com.itykd.web.interceptor.MyInterceptor3"/>
            <!-- 定義攔截器棧 -->
            <interceptor-stack name="myStack">
                <interceptor-ref name="defaultStack"/>
                <interceptor-ref name="myInterceptor1"/>
                <interceptor-ref name="myInterceptor2"/>
                <interceptor-ref name="myInterceptor3"/>
            </interceptor-stack>
        </interceptors>
        <action name="interceptorAction1" class="com.itykd.web.action.InterceptorAction1">
            <result name="success">/demo1.jsp</result>
            <!-- 引入攔截器棧 (一旦引入自定義攔截器,預設攔截器棧的攔截器就不執行了)-->
            <interceptor-ref name="myStack"/>
        </action>
    </package>
</struts>
  • 攔截的Action類 
package com.itykd.web.action;

import com.opensymphony.xwork2.ActionSupport;

public class InterceptorAction1 extends ActionSupport{
	public String execute() {
		System.out.println("InterceptorAction1開始執行了.....");
		
		return SUCCESS;
	}
}