随笔 - 6, 文章 - 4, 评论 - 0, 引用 - 0
数据加载中……

spring拦截器拦截struts

  1. 创建拦截器。
  2. 注册拦截器。
  3. 声明在何处拦截代码。

package ca.nexcel.books.interceptors;
            import org.springframework.aop.MethodBeforeAdvice;
            import java.lang.reflect.Method;
            public class LoggingInterceptor implements MethodBeforeAdvice {
            public void before(Method method, Object[] objects, Object o) throws Throwable {
            System.out.println("logging before!");
            }
            }
            

这个拦截器非常简单。before() 方法在拦截点中每个方法之前运行。在本例中,它打印出一句话,其实它可以做您想做的任何事。下一步就是在 Spring 配置文件中注册这个拦截器,如清单 8 所示:


清单 8. 在 Spring 配置文件中注册拦截器
<?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
            "http://www.springframework.org/dtd/spring-beans.dtd">
            <beans>
            <bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/>
            <bean name="/searchSubmit"
            class="ca.nexcel.books.actions.SearchSubmit">
            <property name="bookService">
            <ref bean="bookService"/>
            </property>
            </bean>
            <!--  Interceptors -->
            <bean name="logger"
            class="ca.nexcel.books.interceptors.LoggingInterceptor"/> |(1)
            <!-- AutoProxies -->
            <bean name="loggingAutoProxy"
            class="org.springframework.aop.framework.autoproxy.
            BeanNameAutoProxyCreator"> |(2)
            <property name="beanNames">
            <value>/searchSubmit</valuesgt; |(3)
            </property>
            <property name="interceptorNames">
            <list>
            <value>logger</value> |(4)
            </list>
            </property>
            </bean>
            </beans>
            

您可能已经注意到了,清单 8 扩展了 清单 6 中所示的应用程序以包含一个拦截器。具体细节如下:

  • 在 (1) 处,我注册了这个拦截器。
  • 在 (2) 处,我创建了一个 bean 名称自动代理,它描述如何应用拦截器。还有其他的方法定义拦截点,但是这种方法常见而简便。
  • 在 (3) 处,我将 Struts 动作注册为将被拦截的 bean。如果您想要拦截其他的 Struts 动作,则只需要在 "beanNames" 下面创建附加的 <value> 标记。
  • 在 (4) 处,当拦截发生时,我执行了在 (1) 处创建的拦截器 bean 的名称。这里列出的所有拦截器都应用于“beanNames”。

posted on 2007-12-23 17:26 小石~~ 阅读(712) 评论(0)  编辑  收藏 所属分类: ssh相关


只有注册用户登录后才能发表评论。


网站导航: