2

我目前正在使用一个配置了 xml 的拦截器,如下所示:

<mvc:interceptors>
    <bean class="org.resthub.dashboard.BasicInterceptor" />
</mvc:interceptors>

但我想把它放在我的配置类中:

    @Configuration
    @ComponentScan("org.resthub.dashboard")
    @EnableWebMvc
    @EnableAspectJAutoProxy(proxyTargetClass=true)
    public class WebAppConfig extends WebMvcConfigurerAdapter{

        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new BasicInterceptor());
        }

        @Bean
        public InternalResourceViewResolver setupViewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/views/");
            resolver.setSuffix(".jsp");
            return resolver;
        }
    }

但它不起作用,我不知道为什么。拦截器永远不会被调用。

有任何想法吗 ?

谢谢

edit: 这里是 BasicInterceptor

    package org.resthub.dashboard;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.springframework.security.core.Authentication;
    import org.springframework.security.core.context.SecurityContextHolder;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

    public class BasicInterceptor extends HandlerInterceptorAdapter {

        @Override
        public void postHandle(HttpServletRequest request,
                HttpServletResponse response, Object handler,
                ModelAndView modelAndView) throws Exception {
            // TODO Auto-generated method stub
            super.postHandle(request, response, handler, modelAndView);

            System.out.println("INTERCEPTORORRR§§§§§§");
            if (modelAndView != null && modelAndView.getModelMap() != null) {
    ...
            }
        }

    }
4

1 回答 1

0

看来您的配置是正确的,至少如果您已验证它实际上已被拾取。但是,可能的问题是在请求期间发生了一些导致postHandle不被执行的事情。确保在请求执行期间没有抛出任何异常。为了验证 Interceptor 是否确实被使用,您可以实现preHandleorafterCompletion并查看这些方法是否被执行。

于 2013-04-18T12:18:09.647 回答