简介
在Struts 中的拦截器要实现comopensymphonyxworkinterceptorInterceptor接口在strutsxml中配置可以用拦截器来完成调用Action业务逻辑之前的预处理或是之后的善后处理还可以通过配置多个拦截器来满足action需求
Interceptor stack是由多个拦截器组成的拦截器组在拦截器组中可以对每一个拦截器映射所有进行配置拦截器时不必对每一个拦截器进行配置而只需对interceptor stack进行配置即可在struts 中默认配置了一个全局interceptor stack包括Exception InterceptorValidation Interceptor等
实例
在这个实例当中我将配置一个时间拦截器用来统计每个action的请求时间
packageinterceptor;
importcomopensymphonyxworkActionInvocation;
importcomopensymphonyxworkinterceptorInterceptor;
/***//**
*authorby;
*/
publicclassActionTimerimplementsInterceptor{
publicStringintercept(ActionInvocationnext)throwsException{
longt=SystemcurrentTimeMillis();
Strings=nextinvoke();
longt=SystemcurrentTimeMillis();
Systemoutprintln(Action+nextgetAction()getClass()getName()+took+(tt)+millisecs);
returns;
}
publicvoidinit(){
}
publicvoiddestroy(){
}
}
strutsxml
<?xmlversion=encoding=UTF?>
<!DOCTYPEstrutsPUBLIC
//ApacheSoftwareFoundation//DTDStrutsConfiguration//EN
dtd>
<struts>
<packagename=interceptorextends=strutsdefault>
<interceptors>
<interceptorname=actiontimer
class=interceptorActionTimer/>
<interceptorstackname=demostack>
<interceptorrefname=defaultStack/>
<interceptorrefname=actiontimer/>
</interceptorstack>
</interceptors>
<defaultinterceptorrefname=demostack/>
<actionname=InterceptorDemo
class=interceptoractionInterceptorDemo>
<result>;/interceptor/interceptordemojsp</result>
</action>
</package>
</struts>
interceptordemojsp
<html>
<head>
</head>
<body>
</body>
</html>