拦截器使用步骤:
1. 创建拦截器。
2. 注册拦截器。
3. 声明在何处拦截代码。
将 Struts 动作委托给 Spring 框架可以将 Spring 的 AOP 拦截器应用于 Struts 动作。
2.5.3. 一个例子:
一个简单的日志记录拦截器
//创建拦截器
public class LoggingInterceptor implements MethodBeforeAdvice { public void before(Method method, Object[] objects, Object o)
throws Throwable {
System.out.println("logging before!");
} }
注册拦截器
<!-- Interceptors --> <!-- 注册了这个刚才建的拦截器 --> <bean name="logger"
class="ca.nexcel.books.interceptors.LoggingInterceptor" />
<!-- AutoProxies -->
<!-- 创建了一个 bean 名称自动代理,它描述如何应用拦截器。还有其他的方法定义拦截点,但是这种方法常见而简便。 -->
<bean name="loggingAutoProxy" class="org.springframework.aop.framework.autoproxy. BeanNameAutoProxyCreator">
<property name="beanNames">
<!-- 将 Struts 动作注册为将被拦截的 bean --> <value>/searchSubmit</value> </property>
<property name="interceptorNames"> <list>
<!-- 当拦截发生时,我执行了在 (1) 处创建的拦截器 bean 的名称 --> <value>logger</value>
</list>
</property>
</bean>