1.拦截器类
package com.xxxxxx.interceptors;
class TestInteceptor implements Interceptor
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("before interceptor...");
final String result = invocation.invoke();
System.out.println("after interceptor...");
return result;
}
2.拦截器配置
<interceptors>
<interceptor name="testInterceptor"
class="com.xxxxxx.interceptors.TestInteceptor">
</interceptor> //声明拦截器
<interceptor-stack name="modifiedStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="testInterceptor"/> //在默认拦截器之后执行
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="modifiedStack"/>//修改使用默认拦截器
3.示例
http://localhost:8080/SSMProject/test
控制台输出:
before interceptor...
after interceptor...
本文介绍了一个Struts2拦截器的具体实现与配置过程。通过定义拦截器类`TestInteceptor`并实现`Interceptor`接口,在类中覆盖`intercept`方法来完成前后拦截操作。此外还详细展示了如何在Struts2配置文件中声明该拦截器,并将其加入到默认拦截器栈中以确保被正确调用。
1358

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



