struts2系统自带了很多拦截器,有时需要我们自己定义,一般有两种方式:
一、实现Interceptor接口
public interface Interceptor extends Serializable{
public void init();
public void destroy();
public String intercept(ActionInvocation invocation)();
}
实现上述方法
二、继承AbstractInterceptor类,重写intercept()方法即可
此方法更可行,其实AbstractInterceptor类也就是实现了Interceptor接口
查看源码打印?
invocation.invoke();表示该方法执行完后执行Action的execute()方法或者执行下一个拦截器
invocation.getAction(); 可以将该法强制转换为Action的类类型
三、方法拦截器:继承MethodFilterInterceptor类,重写doIntercept()方法
MethodFilerInterceptor实现方法过滤中用到的两个参数
execludeMethods:该参数指定拦截器拒绝拦截的方法列表,多个方法用“,”隔开,指定了这个参数,
拦截器不会拦截指定列表中的方法,就是所谓的黑名单
includeMethods:该参数指定拦截器需要拦截的方法列表,如果指定了参数,则指定的Action在执行前会被拦截,
即白名单。
定义好自定义拦截器后,就要使用自定义拦截器,在struts.xml文档中
一、包内定义拦截器
查看源码打印?
<package....>
<interceptors>
<!-- 自定义拦截器 start -->
<interceptor name="myinterceptor01" class="....01"/>
<interceptor name="myinterceptor02" class="....02"/>
<!-- 自定义拦截器 end -->
<!-- 拦截器栈 start -->
<interceptor-stack name="sysModuleStack">
<interceptor-ref name="authority" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
<interceptor-stack name="toOnLineExamStack">
<interceptor-ref name="toOnLineExamInterceptor" />
<interceptor-ref name="sysModuleStack" />
</interceptor-stack>
<!-- 拦截器栈 end -->
</interceptors>
</package>
二、action内使用拦截器
查看源码打印?
<action .....>
<result.....></result>
<!-- 自定义拦截器使用 start -->
<interceptor-ref name="defaultStack" />
<interceptor-ref name="myinterceptor01" />
</action>
主要:可以看出使用了自定义拦截器的action要配置默认拦截器的引用,因为默认拦截器包含了参数的读取、session的管理等功能一下是例子:
myIntersetor01.java
public class MyMethodInterceptor extends MethodFilterInterceptor{
protected String doIntercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
System.out.println("进入MyMethodInterceptor方法拦截器!!!!!!!!!!!!!");
Map session = invocation.getInvocationContext().getSession();
String name = (String) session.get("uname");
if (name != null) {
return invocation.invoke();
}
return "input";
}
}
myIntersetor02.java
package com.eems.exam.intercepter;
import java.util.Map;
import com.bean.Examinee;
import com.eems.exam.testmanage.OnlineExam.action.OnlineExaminneAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class ToOnLineExamInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext ctx=invocation.getInvocationContext();
Map session=ctx.getSession();
Examinee exam = (Examinee) session.get("examinee");
if(exam == null){
ctx.put("tip","您还没有登录,请登陆系统");
return "HOMELOGIN";
}
OnlineExaminneAction action = (OnlineExaminneAction)invocation.getAction();
String testStuate = action.checkTestStuate(exam);
if("0".equals(testStuate)){
ctx.put("tip","您已考试通过,请勿重复考试。");
return "examinneRule";
}
return invocation.invoke();
}
}
strtus.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 权限控制 -->
<package name="authority" extends="struts-default">
<!-- 定义一个拦截器 -->
<interceptors>
<interceptor name="authority"
class="com.eems.exam.intercepter.AuthorityInterceptor">
</interceptor>
<interceptor name="toOnLineExamInterceptor"
class="com.eems.exam.intercepter.ToOnLineExamInterceptor">
</interceptor>
<!-- 拦截器栈 -->
<interceptor-stack name="sysModuleStack">
<interceptor-ref name="authority" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
<interceptor-stack name="toOnLineExamStack">
<interceptor-ref name="toOnLineExamInterceptor" />
<interceptor-ref name="sysModuleStack" />
</interceptor-stack>
</interceptors>
<!-- 考生信息 -->
<action name="stuInfo" class="StuInfoAction"
method="toStuInfo">
<result name="success">/webapp/myregister/stuInfo.jsp</result>
<!-- 使用此拦截器 -->
<interceptor-ref name="sysModuleStack" />
</action>
<!-- 初始考试界面 -->
<action name="toOnlineExaminne" class="OnlineExam" method="toMockExaminne">
<result name="onlineExameView">/onlineexame/onlineexame.jsp</result> <!--初始考试界面 -->
<result name="examinneRule">/onlineexame/result/pass.jsp</result>
<!-- 使用此拦截器 -->
<interceptor-ref name="toOnLineExamStack" />
</action>
<!-- 上一页或下一页考试界面 -->
<action name="toNextOnlineExaminne" class="OnlineExam" method="toNextMockExaminne">
<result name="nextOlineExameView">/onlineexame/onlineexame.jsp</result>
<result name="examinneRule">/onlineexame/result/pass.jsp</result>
<!-- 使用此拦截器 -->
<interceptor-ref name="toOnLineExamStack" />
</action>
<!-- 得分界面 -->
<action name="toOverOlineExame" class="OnlineExam" method="getOverExam">
<result name="overOnlineExaminneView">/onlineexame/result/onlineresult.jsp</result>
<result name="examinneRule">/onlineexame/result/pass.jsp</result>
<!-- 使用此拦截器 -->
<interceptor-ref name="toOnLineExamStack" />
</action>
</
package
>
</
struts
>