1.使用拦截器的顺序
1.确保引入xwork-core-2.3.24.1.jar
2.定义拦截器,自己创建的.java文件
3.注册拦截器和引用拦截器,在struts.xml中
2.默认拦截器栈
*.在没有自定义拦截器时,默认拦截自动拦截,无需手动显示的引用
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*</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-stack>
*.若有自定义拦截器,需要手动添加,否则会出错
·以上传为例
<action name="upload" class="com.action.UploadAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">text/plain,image/jpeg</param>
<param name="maximumSize">1048576</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<param name="savePath">/upload</param>
<result>success.jsp</result>
<result name="input">uploadWithInterceptor.jsp</result>
</action>
3.自定义拦截器栈
1.定义拦截器MyInterceptor1.java 实现 Interceptor 接口
package com.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
//定义拦截器
public class MyInterceptor1 implements Interceptor {
private String interceptorName;
public String getInterceptorName() {
return interceptorName;
}
public void setInterceptorName(String interceptorName) {
this.interceptorName = interceptorName;
}
public void destroy() {
System.out.println("MyInterceptor1 destroy");
}
public void init() {
System.out.println("MyInterceptor1 init");
}
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Interceptor name: " + getInterceptorName());
System.out.println("before " + interceptorName + " intercept");
String result = invocation.invoke();
System.out.println("after " + interceptorName + " intercept");
return result;
}
}
2.自定义拦截器MyInterceptor2继承AbstractInterceptor类
复写:intercept(ActionInvocation invocation)方法
package com.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class MyInterceptor2 extends AbstractInterceptor {
private String interceptorName;
public String getInterceptorName() {
return interceptorName;
}
public void setInterceptorName(String interceptorName) {
this.interceptorName = interceptorName;
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Interceptor name: " + interceptorName);
System.out.println("before " + interceptorName +" intercept");
String result = invocation.invoke();
System.out.println("after " + interceptorName + " intercept");
return result;
}
}
3.方法过滤拦截器MyMethodInterceptor 继承MethodFilterInterceptor类
复写:doIntercept(ActionInvocation invocation)方法
package com.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class MyMethodInterceptor extends MethodFilterInterceptor {
private String interceptorName;
public String getInterceptorName() {
return interceptorName;
}
public void setInterceptorName(String interceptorName) {
this.interceptorName = interceptorName;
}
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("Interceptor name: " + getInterceptorName());
System.out.println("before " + getInterceptorName() + " intercept");
String result = invocation.invoke();
System.out.println("after " + getInterceptorName() + " intercept");
return result;
}
}
4.注册拦截器和直接引用
在struts.xml文件中配置
* 拦截器一般用法
<package name="struts2" extends="struts-default">
<!--所有拦截器需在<interceptors>标签中配置-->
<!-- 注册拦截器 -->
<interceptors>
<interceptor name="myInterceptor1" class="com.interceptor.MyInterceptor1">
<param name="interceptorName">myInterceptor1</param>
</interceptor>
<interceptor name="myInterceptor2" class="com.interceptor.MyInterceptor2">
<param name="interceptorName">myInterceptor2</param>
</interceptor>
<interceptor name="MyMethodInterceptor" class="com.interceptor.MyMethodInterceptor">
<param name="interceptorName">MyMethodInterceptor</param>
<param name="excludeMethods">register</param>
</interceptor>
<!-- 引用拦截器 -->
<action name="login" class="com.interceptor.LoginAction" method="login">
<interceptor-ref name="myInterceptor1"></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>
<interceptor-ref name="MyMethodInterceptor"></interceptor-ref>
<!-- 不加默认会出错 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
<result>main.jsp</result>
<result name="input">login.jsp</result>
</action>
</package>
*配置拦截器栈
<interceptors>
<!--(小编省略了刚刚注册的拦截器),引用的话要复制进来-->
<interceptor-stack name="myInterceptorStack">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>
<interceptor-ref name="MyMethodInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 引用拦截器 -->
<action name="login" class="com.interceptor.LoginAction" method="login">
<!--这里只要写这个拦截器栈名就可以了-->
<interceptor-ref name="myInterceptorStack"></interceptor-ref>
<result>main.jsp</result>
<result name="input">login.jsp</result>
</action>
</package>
*默认拦截器
一般默认拦截器都是我们配置好的拦截器栈,这样既满足我们的需求,也可以简化我们的代码
将我们上述的拦截器栈设为默认,因此我们就不必再action里配置拦截器,代码简洁很多了
<!-- 配置默认拦截器-->
<default-interceptor-ref name="myInterceptorStack"></default-interceptor-ref>
<!-- 默认拦截器,无需手动引用 -->
<action name="login" class="com.interceptor.LoginAction" method="login">
<result>main.jsp</result>
<result name="input">login.jsp</result>
</action>