import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
/**
* 定义自己的拦截器,需要实现Intercept接口!
*
*/
public class MyInterceptor implements Interceptor{
public void destroy()
{
}
public void init()
{
}
/**
* 重写intercept方法,在该方法中实现自己的拦截逻辑!
* 调用invocation.invoke()方法放行action!
*/
public String
intercept(ActionInvocation invocation) throws Exception
{
System. out.println("图片上传" );
return invocation.invoke();
// return null;//不执行action
}
}
|
<?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="interceptor" namespace="/" extends="struts-default" >
<!-- 声明拦截器,拦截器声明应该位于action之前-->
<interceptors>
<!--声明自己定义的拦截器
-->
<interceptor name= "imageInterceptor"
class= "cn.itheima03.struts2.interceptor.MyInterceptor" ></interceptor>
<!-- 声明拦截器栈
-->
<interceptor-stack name= "myInterceptor">
<interceptor-ref name="imageInterceptor" ></interceptor-ref>
<interceptor-ref name="defaultStack" ></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 修改默认的拦截器栈
-->
<default-interceptor-ref name="myInterceptor" ></default-interceptor-ref>
<!--声明action,在执行action之前,会先执行拦截器中的方法
-->
<action name= "interceptorAction_*" method ="{1}"
class= "cn.itheima03.struts2.interceptor.InterceptorTestAction" >
<result>
index.jsp
</result>
</action>
</package >
</struts>
|
import com.opensymphony.xwork2.ActionSupport;
public class InterceptorTestAction extends ActionSupport{
public String
interceptor(){
System. out.println("interceptor" );
return SUCCESS ;
}
}
|
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/damogu_arthur/article/details/46907207