Interceptor 作用:在Action执行前或执行后,执行某些代码,也以决定是否继续调用其它Interceptor及Action.
public class TestInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
//在之后的Interceptor和Action执行前,可以执行某些代码
String resultCode=ActionSupport.INPUT;
if(true){
resultCode = invocation.invoke();//继续执行之后的Interceptor和Action
}else{
//也可以不执行之后的Interceptor和Action,直接跳转到其它页面。
}
//在Interceptor和Action执行完成之后,可以执行某此代码。
return resultCode;
}
}
Struts 中默认执行的interceptor在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">^class\..*,^dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,^parameters\..*,^action:.*,^method:.*</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-ref name="deprecation"/> </interceptor-stack> <default-interceptor-ref name="defaultStack"/>
interceptor的执行顺序与下面定义的顺序一至,最后一个interceptor执行完成后之后,调用Action.