Struts2的拦截器(interceptor)
作用:当请求进入struts2框架后,想对请求进行拦截操作(功能增强、权限控制),需要拦截器组件
1、struts2内置拦截器
1.1 struts-default.xml中配置好的拦截器
struts2框架已经定义好并使用的拦截器
发送请求 -----> interceptor1---->interceptor2 -----> DemoAction
在struts-default.xml文件中已经声明了多个拦截器
<?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="test" namespace="/" extends="struts-default">
<interceptors>
... ...
<!--声明拦截器-->
<interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>
... ...
<!--声明拦截器栈-->
<interceptor-stack name="basicStack">
... ...
<interceptor-ref name="exception"/>
... ...
</interceptor-stack>
</interceptors>
<!-- 默认的拦截器引用 -->
<default-interceptor-ref name="defaultStack"/>
</package>
</struts>
1.2 源码分析interceptor的执行过程
--->StrutsPrepareAndExecuteFilter
--->public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
--->execute.executeAction(request, response, mapping);
--->dispatcher.serviceAction(request, response, mapping);
--->ActionProxy proxy = createActionProxy();
--->proxy.execute();
--->retCode = invocation.invoke();
--->
Iterator<InterceptorMapping> interceptors;
//判断迭代器集合中是否还有下一个
if (interceptors.hasNext()) {
final InterceptorMapping interceptor = interceptors.next(); //获得当前InterceptorMapping对象
resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
获得拦截器对象本身 调用该拦截器内部的拦截方法intercept
} else {
resultCode = invokeActionOnly(); //仅仅执行当前要访问目标Action
}
PS:
resultCode:逻辑视图名,字符串
总结:
如果在interceptor中调用invocation.invoke(); 代表放行
如果在interceptor中return 字符串,代表不放行 , 且return的字符串就是要匹配的逻辑视图
PS:
<interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>
class InterceptorMapping{
private String name; //modelDriven
private Interceptor interceptor; //class对应全限定名的对象
}
2、struts2自定义拦截器
开发步骤:
1、自定义拦截器类 实现interceptor接口(继承interceptor的实现类)
2、实现intercept方法,该方法封装拦截功能的逻辑代码
public class MyInterceptor1 implements Interceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("MyInterceptor1执行...");
return invocation.invoke();
}
@Override
public void destroy() {}
@Override
public void init() {}
}
public class MyInterceptor2 extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("MyInterceptor2执行................");
return invocation.invoke();
}
}
public class MyInterceptor3 extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("MyInterceptor33333执行.........");
return invocation.invoke();
}
}
3、在struts.xml中配置拦截器
声明拦截器
使用拦截器
<?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>
<constant name="struts.devMode" value="true"></constant>
<package name="test" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="my2" class="com.itheima.action.interceptor.MyInterceptor2"></interceptor>
<interceptor name="my1" class="com.itheima.action.interceptor.MyInterceptor1"></interceptor>
<interceptor name="my3" class="com.itheima.action.interceptor.MyInterceptor3"></interceptor>
<interceptor-stack name="my">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="my1"></interceptor-ref>
<interceptor-ref name="my2"></interceptor-ref>
<interceptor-ref name="my3">
<param name="excludeMethods">xxx</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<!--
细节点:拦截器的作用范围
1、当前包下所有的Action都执行配置的拦截器
<default-interceptor-ref name="my"></default-interceptor-ref>
2、action可以执行自己局部的interceptor
<action name="target_*" class="com.itheima.action.TargetAction" method="{1}">
<interceptor-ref name="my"></interceptor-ref>
<result name="success">/success.jsp</result>
</action>
3、action中的方法可以执行局部的interceptor
例如:访问target的show方法执行my拦截器 访问target的xxx方法不执行my拦截器
class MyInterceptor3 extends MethodFilterInterceptor
<interceptor-ref name="my3">
<param name="excludeMethods">xxx</param>
</interceptor-ref>
-->
<action name="target_*" class="com.itheima.action.TargetAction" method="{1}">
<interceptor-ref name="my"></interceptor-ref>
<result name="success">/success.jsp</result>
</action>
<action name="target2_*" class="com.itheima.action.TargetAction2" method="{1}">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
4、测试(访问action)
public class TargetAction extends ActionSupport {
private String name;
public void setName(String name) {
this.name = name;
}
public String show(){
System.out.println(name);
return SUCCESS;
}
public String xxx(){
return SUCCESS;
}
}
public class TargetAction2 extends ActionSupport {
public String show(){
return SUCCESS;
}
}