拦截器是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,只会跳转页面,没有拦截