目錄
攔截器的創建
創建方式1
實現Interceptor接口
package cn.ycsj.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
//攔截器,創建方式1
//攔截器的生命週期:隨項目的啓動而創建,隨項目的關閉而銷燬 (servlet 第一次訪問創建 項目關閉銷燬)
public class MyInterceptor1 implements Interceptor {
/**
* 銷燬
*/
@Override
public void destroy() {
}
/**
* 初始化
*/
@Override
public void init() {
}
/**
* 攔截方法
* @param actionInvocation
* @return
* @throws Exception
*/
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
return null;
}
}
創建方式2
繼承AbstractInterceptor
package cn.ycsj.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
//攔截器,創建方式2
//繼承AbstractInterceptor
//攔截器的生命週期:隨項目的啓動而創建,隨項目的關閉而銷燬 (servlet 第一次訪問創建 項目關閉銷燬)
public class MyInterceptor2 extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
return null;
}
}
創建方式3(推薦)
繼承MethodFilterInterceptor
package cn.ycsj.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
//攔截器,創建方式3
//繼承MethodFilterInterceptor
//功能定製攔截器攔截的方法。
// 定製哪些方法需要攔截,
// 定製哪些方法不需要攔截。
public class MyInterceptor3 extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation Invocation) throws Exception {
//前處理
System.out.println("MyInterceptor3 前處理 。。。。");
//放行
Invocation.invoke();
//後處理
System.out.println("MyInterceptor3 後處理 。。。。");
return null;
}
}
放行Invocation.invoke();
不放行 不執行Invocation.invoke(); return "xxx"; 就會不執行後續的攔截器以及action 直接跳轉跳過頁面
攔截器的配置
1.創建一個攔截器
package cn.ycsj.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
//攔截器,創建方式3
//繼承MethodFilterInterceptor
//功能定製攔截器攔截的方法。
// 定製哪些方法需要攔截,
// 定製哪些方法不需要攔截。
public class MyInterceptor3 extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation Invocation) throws Exception {
//前處理
System.out.println("MyInterceptor3 前處理 。。。。");
//放行
String result = Invocation.invoke();
//後處理
System.out.println("MyInterceptor3 後處理 。。。。");
return result;
}
}
2.配置攔截器
參考struts-default.xml文件
在struts.xml中配置
<package name="results" namespace="/" extends="struts-default">
<interceptors>
<!-- 1.註冊攔截器-->
<interceptor name="MyInterceptor3" class="cn.ycsj.interceptor.MyInterceptor3"></interceptor>
<!-- 2.註冊攔截器棧-->
<interceptor-stack name="myStack">
<!--自定義的攔截器引入 (建議放在20個攔截器之前 ) -->
<interceptor-ref name="MyInterceptor3"/>
<!--需要添加默認攔截器棧 引用默認的攔截器棧(20個) -->
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<!-- 3.指定包中默認攔截器棧-->
<default-interceptor-ref name="myStack"/>
也可以在單獨的action中配置 指定只有某個action走攔截器(配置的時候就不需要指定包中的攔截器棧)
<action name="UserAction_*" class="cn.ycsj.struts.UserAction" method="{1}">
<!--爲單獨的action執行走哪個攔截器(棧)-->
<!-- <interceptor-ref name="myStack"/>-->
<result name="tohome" type="redirect">
/index.jsp
</result>
<result name="error">/login.jsp</result>
</action>
攔截器攔截方法指定,定製攔截器
1.創建一個action
package cn.ycsj.struts;
import com.opensymphony.xwork2.ActionSupport;
public class Demo1Action extends ActionSupport {
public String add() throws Exception {
System.out.println("demo1_add");
return SUCCESS;
}
public String delete() throws Exception {
System.out.println("demo1_delete");
return "xxx";
}
public String update() throws Exception {
System.out.println("demo1_update");
return SUCCESS;
}
public String select() throws Exception {
System.out.println("demo1_select");
return SUCCESS;
}
}
2.配置struts.xml中的攔截器
注意不攔截配置<param name="excludeMethods">add,delete</param>
和攔截配置<param name="includeMethods"></param>不能同時使用 ,只能二選一
<package name="results" namespace="/" extends="struts-default">
<interceptors>
<!-- 1.註冊攔截器-->
<interceptor name="MyInterceptor3" class="cn.ycsj.interceptor.MyInterceptor3"></interceptor>
<!-- 2.註冊攔截器棧-->
<interceptor-stack name="myStack">
<!--自定義的攔截器引入 (建議放在20個攔截器之前 ) -->
<interceptor-ref name="MyInterceptor3">
<!--指定哪些方法不攔截 攔截和不攔截不能同時使用 -->
<param name="excludeMethods">add,delete</param>
<!--指定攔截 <param name="includeMethods"></param> -->
</interceptor-ref>
<!--需要添加默認攔截器棧 引用默認的攔截器棧(20個) -->
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<!-- 3.指定包中默認攔截器棧-->
<default-interceptor-ref name="myStack"/>
<action name="Demo1Action_*" class="cn.ycsj.struts.Demo1Action" method="{1}">
<result name="success" >
/index.jsp
</result>
<result name="xxx">/form2.jsp</result>
</action>
</package>
<!--配置爲true,熱加載配置文件,日誌信息更加詳細-->
<constant name="struts.devMode" value="true"/>
</struts>
訪問控制檯結果如下
demo1_delete
MyInterceptor3 前處理 。。。。
demo1_select
MyInterceptor3 後處理 。。。。
驗證登錄攔截器的實現
攔截器創建
public class LoginInterceptor extends MethodFilterInterceptor {
/**
* 指定不攔截登錄方法,其他的方法都需要攔截登錄
* @param invocation
* @return
* @throws Exception
*/
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
//1.獲取session
Map<String, Object> session = ActionContext.getContext().getSession();
//2.獲得登錄標識
Object user = session.get("user");
//3.判斷登錄標識是否存在
//不存在=>沒登錄=>重定向到登錄界面
//存在=>已經登錄 =>放行
if (user ==null){
return "toLogin";
}else {
return invocation.invoke();
}
}
}
配置
<?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="results" namespace="/" extends="struts-default">
<interceptors>
<!-- 1.註冊攔截器-->
<interceptor name="loginInterceptor" class="cn.ycsj.interceptor.LoginInterceptor"></interceptor>
<!-- 2.註冊攔截器棧-->
<interceptor-stack name="myStack">
<!--自定義的攔截器引入 (建議放在20個攔截器之前 ) -->
<interceptor-ref name="loginInterceptor">
<!--指定哪些方法不攔截 攔截和不攔截不能同時使用 -->
<param name="excludeMethods">add,delete</param>
<!--指定攔截 <param name="includeMethods"></param> -->
</interceptor-ref>
<!--需要添加默認攔截器棧 引用默認的攔截器棧(20個) -->
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<!-- 3.指定包中默認攔截器棧-->
<default-interceptor-ref name="myStack"/>
<!--定義全局結果集 -->
<global-results>
<result name="toLogin" type="redirect">/login.jsp </result>
</global-results>
<!--如果出現RuntimeException 這個異常 就將跳轉到名爲error的結果 需要配置結果集 -->
<global-exception-mappings>
<exception-mapping exception="java.lang.RuntimeException" result="error"></exception-mapping>
</global-exception-mappings>
<action name="UserAction_*" class="cn.ycsj.struts.UserAction" method="{1}">
<!--爲單獨的action執行走哪個攔截器(棧)-->
<!-- <interceptor-ref name="myStack"/>-->
<result name="tohome" type="redirect">
/index.jsp
</result>
<result name="error">/login.jsp</result>
<result name="success">/form2.jsp</result>
</action>
</package>
<!--配置爲true,熱加載配置文件,日誌信息更加詳細-->
<constant name="struts.devMode" value="true"/>
</struts>
全局結果集的定義省去了在每個action中都需要配置result的煩惱
<global-results>
<result name="toLogin" type="redirect">/login.jsp </result>
</global-results>