拦截器的执行顺序:
一个拦截器:textOne.java
public class textOne extends AbstractInterceptor {
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("拦截器11111前");
String result = invocation.invoke();
System.out.println("拦截器11111后");
return result;
}
}
另一个拦截器:textTwo.java
public class textTwo extends AbstractInterceptor{
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("拦截器22222前");
String result = invocation.invoke();
System.out.println("拦截器22222后");
return result;
}
}
随便一个页面:
/**
* 转向登录页面
*/
public String goLogin() throws Exception {
System.out.println("你好!《》《》《》");
roleList = roleService.getRoles();
String forward = AppConstants.RETURN_TYPE_FREEMARKER;
return forward;
}
在struts.xml中配置好拦截器
<interceptors>
<interceptor name="userSession" class="com.yuanit.app.interceptor.UserSessionInterceptor"/>
<interceptor name="logInterceptor" class="com.yuanit.app.interceptor.LogInterceptor"/>
<interceptor name="textOne" class="com.yuanit.app.interceptor.textOne"/>
<interceptor name="textTwo" class="com.yuanit.app.interceptor.textTwo"/>
<interceptor-stack name="interceptorStack">
<interceptor-ref name="logInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<action name="*_*" class="com.yuanit.app.action.{1}Action" method="{2}">
<result name="json" type="json">
<param name="includeProperties">
resultInfo/.success,resultInfo/.message
</param>
</result>
<result name="freemarker" type="freemarker">template/app/{1}/{2}.ftl</result>
<interceptor-ref name="textOne" />
<interceptor-ref name="textTwo" />
</action>
这样配置后,运行程序结果是:
拦截器11111前
拦截器22222前
你好!《》《》《》
拦截器22222后
拦截器11111后
拦截器的运行顺序!!!