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.

本文详细介绍了Struts框架中拦截器的概念、作用及其在默认配置下的执行顺序,包括如何在Action执行前后插入自定义逻辑,以及默认配置中包含的多个拦截器及其各自的功能。
1700

被折叠的 条评论
为什么被折叠?



