struts2架構學習筆記6:攔截器,struts2學習筆記
攔截器是Struts2實現功能的核心部分
攔截器的建立:
第一種:
package interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;//攔截器的第一種建立方式//攔截器的生命週期:隨項目啟動建立,隨項目關閉而銷毀public class MyInterceptor implements Interceptor { @Override public void destroy() { // 銷毀 } @Override public void init() { // 初始化 } @Override public String intercept(ActionInvocation arg0) throws Exception { // 攔截方法 return null; }}
第二種:
(原理上和和第一種相同,只是空實現了init和destroy方法)
package interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;//第二種建立方式public class MyInterceptor2 extends AbstractInterceptor { @Override public String intercept(ActionInvocation arg0) throws Exception { // 攔截方法 return null; }}
第三種(推薦):
package interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;//方法過濾攔截器:定製攔截的方法public class MyInterceptor3 extends MethodFilterInterceptor { @Override protected String doIntercept(ActionInvocation arg0) throws Exception { return null; }}
攔截器的API:
package interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;//方法過濾攔截器:定製攔截的方法public class MyInterceptor3 extends MethodFilterInterceptor { @Override protected String doIntercept(ActionInvocation invocation) throws Exception { //前處理 System.out.println("前處理"); //允許存取 String result = invocation.invoke(); //後處理 System.out.println("後處理"); return result; }}
攔截器的配置:
簡單寫一個Action:
package interceptor;import com.opensymphony.xwork2.ActionSupport;public class Demo1Action extends ActionSupport { public String add() throws Exception { System.out.println("Demo1Action_add!"); return SUCCESS; } public String delete() throws Exception { System.out.println("Demo1Action_delete!"); return SUCCESS; } public String update() throws Exception { System.out.println("Demo1Action_update!"); return SUCCESS; } public String find() throws Exception { System.out.println("Demo1Action_find!"); return SUCCESS; } }
配置:
<?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="inter" namespace="/" extends="struts-default" ><interceptors> <!-- 1.註冊攔截器 --> <interceptor name="myInter3" class="interceptor.MyInterceptor3"></interceptor> <!-- 2.註冊攔截器棧 --> <interceptor-stack name="myStack"> <!-- 自訂攔截器引入(建議放在20個預設攔截器之前) --> <interceptor-ref name="myInter3"> <!-- 指定哪些方法不攔截 <param name="excludeMethods">add,delete</param> --> <!-- 指定哪些方法需要攔截 --> <param name="includeMethods">add,delete</param> </interceptor-ref> <!-- 引用預設的攔截器棧(20個) --> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 3.指定包中的預設攔截器棧 --> <default-interceptor-ref name="myStack"></default-interceptor-ref> <action name="Demo1Action_*" class="interceptor.Demo1Action" method="{1}" > <!-- 為Action單獨指定走哪個攔截器(棧) <interceptor-ref name="myStack"></interceptor-ref>--> <result name="success" type="dispatcher" >/hello.jsp</result> </action> </package></struts>
全域配置:
<global-results> <!-- 全域結果集配置 --> <result name="toLogin" type="redirect">/login.jsp</result> </global-results> <global-exception-mappings> <!-- 如果出現java.lang.RuntimeException異常,就將跳轉到名為error的結果 --> <exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping> </global-exception-mappings>
這時候訪問:http://localhost:8080/struts2/Demo1Action_add時候,控制台列印,頁面跳轉hello.jsp
前處理
Demo1Action_add!
後處理
如果訪問http://localhost:8080/struts2/Demo1Action_find,只會跳轉頁面,沒有攔截