一、拦截器与过滤器的区别
二、Interceptor的实现
1、写一个类继承com.opensymphony.xwork2.interceptor.AbstractInterceptor
在org.zttc.itat.interceptor包下新建类HelloInterceptor.java并继承com.opensymphony.xwork2.interceptor.AbstractInterceptor
package org.zttc.itat.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/*
* 创建一个类继承AbstractInterceptor
*/
public class HelloInterceptor extends AbstractInterceptor {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("system interceptors");
return invocation.invoke();
}
}
2、配置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.action.extension" value="action,ejb,html"/>
<constant name="struts.configuration.xml.reload" value="true"/>
<constant name="struts.multipart.maxSize" value="10000000"/>
<!-- ?表示0次或一次 +表示一次或多次 *表示0次或多次 -->
<package name="default" namespace="/" extends="struts-default">
<!-- 创建了一个拦截器的配置,但是此时拦截器并没有生效,需要在action中引入才能生效 -->
<interceptors>
<interceptor name="helloInterceptor" class="org.zttc.itat.interceptor.HelloInterceptor"></interceptor>
</interceptors>
<action name="*_*" class="org.zttc.itat.action.{1}Action" method="{2}">
<!-- 在action中引入相应的拦截器 -->
<interceptor-ref name="helloInterceptor"/>
<result name="success">/WEB-INF/{1}/{2}.jsp</result>
</action>
</package>
</struts>
3、在浏览器中输入:http://localhost:8080/struts01_10/Message_addInput.action
输入数据,点击提交,然后跳转到 http://localhost:8080/struts01_10/Message_add.action
没有显示表单数据。
在struts.xml文档中注释掉拦截器,再访问却可以得到数据。
为什么?
查看struts-default.xml。我们发现了如下的代码:
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="multiselect"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="debugging"/>
</interceptor-stack>
<!-- The completeStack is here for backwards compatibility for
applications that still refer to the defaultStack by the
old name -->
<interceptor-stack name="completeStack">
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
<!-- Sample execute and wait stack.
Note: execAndWait should always be the *last* interceptor. -->
<interceptor-stack name="executeAndWaitStack">
<interceptor-ref name="execAndWait">
<param name="excludeMethods">input,back,cancel</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="execAndWait">
<param name="excludeMethods">input,back,cancel</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="defaultStack"/>
<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
从代码中我们了解到struts的所有操作都要默认通过一个拦截器Stack(defaultStack),不然会访问不到数据。
4、1所以我们在struts.xml中
<interceptor-ref name="helloInterceptor"/>这句话下面添加如下语句
<interceptor-ref name="defaultStack"/>
再访问即可。
4、2我们可以自己配置自己的拦截器栈,然后添加defaultStack进去。
<?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.action.extension" value="action,ejb,html"/>
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- ?表示0次或一次 +表示一次或多次 *表示0次或多次 -->
<package name="default" namespace="/" extends="struts-default">
<!-- 创建了一个拦截器的配置,但是此时拦截器并没有生效,需要在action中引入才能生效 -->
<interceptors>
<interceptor name="helloInterceptor" class="org.zttc.itat.interceptor.HelloInterceptor"></interceptor>
<!-- 一个拦截器栈可以加入多个拦截器 -->
<interceptor-stack name="helloStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="helloInterceptor"/>
</interceptor-stack>
</interceptors>
<action name="*_*" class="org.zttc.itat.action.{1}Action" method="{2}">
<!-- 在action中引入相应的拦截器 -->
<interceptor-ref name="helloStack"/>
<result name="success">/WEB-INF/{1}/{2}.jsp</result>
<result type="redirect" name="r_list">/{1}_list.action</result>
</action>
</package>
</struts>
三、拦截器实现简单权限控制