八、Struts2自定義攔截器和配置
阿新 • • 發佈:2019-01-09
什麼是攔截器
攔截器就是AOP(Aspect-Oriented Programming)的一種實現(AOP是指用於在某個方法或欄位被訪問之前,進行攔截然後在之前或之後加入某些操作),Struts2中的攔截器只攔截Action類中的某些方法,不能攔截JSP。
Struts2攔截器採用責任鏈模式,在責任鏈模式裡,很多物件由每一個物件對其下家的引用而連線起來形成一條鏈, 責任鏈每一個節點,都可以繼續呼叫下一個節點,也可以阻止流程繼續執行,在Struts2 中可以定義很多個攔截器,將多個攔截器按照特定順序 組成攔截器棧 (順序呼叫棧中的每一個攔截器 )。
預設的攔截器棧
Struts2的預設攔截器棧叫 defaultStack,定義在struts-default.xml中
攔截器和過濾器的區別
1)攔截器是基於JAVA反射機制的,而過濾器是基於函式回撥的
2)過濾器依賴於Servlet容器,而攔截器不依賴於Servlet容器
3)攔截器只能對Action請求起作用(Action中的方法),而過濾器可以對幾乎所有的請求起作用(CSS JSP JS)
自定義攔截器
編寫攔截器,需要實現Interceptor介面,實現介面中的三個方法即init、destroy和intercept。
但是我們可以繼承該介面的實現類,例如AbstractInterceptor,這樣只需要重寫intercept方法即可,檢視其原始碼便可知曉:
/*
* Copyright 2002-2006,2009 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.opensymphony.xwork2.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
/**
* Provides default implementations of optional lifecycle methods
*/
public abstract class AbstractInterceptor implements Interceptor {
/**
* Does nothing
*/
public void init() {
}
/**
* Does nothing
*/
public void destroy() {
}
/**
* Override to handle interception
*/
public abstract String intercept(ActionInvocation invocation) throws Exception;
}
下面是建立攔截器的程式碼示例:
package blog.csdn.net.mchenys;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class MyIntercept extends AbstractInterceptor{
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Action方法執行之前...");
// 執行下一個攔截器
String result = invocation.invoke();
System.out.println("Action方法執行之後...");
return result;
}
}
配置攔截器
當攔截器定義好後,需要在struts.xml中進行攔截器的配置,配置一共有兩種方式:
方式1:配置單個攔截器
<?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>
<package name="default" namespace="/" extends="struts-default">
<!-- 定義攔截器 -->
<interceptors>
<!-- name:攔截器的名稱 class:攔截器的全路徑名稱 -->
<interceptor name="DemoInterceptor" class="blog.csdn.net.mchenys.MyIntercept" />
</interceptors>
<!-- 使用攔截器 -->
<action name="regist1" class="blog.csdn.net.mchenys.Regist1Action">
<!-- 只要是引用自己的攔截器,預設棧的攔截器就不執行了,必須要手動引入預設棧 -->
<interceptor-ref name="DemoInterceptor" />
<!-- 還得引入預設的攔截器 -->
<interceptor-ref name="defaultStack" />
</action>
</package>
</struts>
方式2:配置攔截器棧
<?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>
<package name="default" namespace="/" extends="struts-default">
<!-- 定義攔截器棧 -->
<interceptors>
<!-- name:攔截器的名稱 class:攔截器的全路徑名稱 -->
<interceptor name="DemoInterceptor" class="blog.csdn.net.mchenys.MyIntercept" />
<!-- 定義攔截器棧 -->
<interceptor-stack name="myStack">
<!-- 引入自定義的攔截器 -->
<interceptor-ref name="DemoInterceptor" />
<!-- 引入預設的攔截器 -->
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<action name="regist2" class="blog.csdn.net.mchenys.Regist2Action" >
<!-- 引入攔截器棧 -->
<interceptor-ref name="myStack"/>
</action>
</package>
</struts>