Spring 3.1 HandlerInterceptor未被调用 [英] Spring 3.1 HandlerInterceptor Not being called

查看:1223
本文介绍了Spring 3.1 HandlerInterceptor未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照HandlerInterceptors的文档进行操作。注意到在新版本的Spring中:配置的拦截器将应用于使用带注释的控制器方法处理的所有请求。

I followed the documentation for HandlerInterceptors. Noting that in the new version of Spring: "the configured interceptor will apply to all requests handled with annotated controller methods".

以下是在xml配置文件中:

The following is in an xml configuration file:

我有一个带注释的控制器,如下所示:

I have an annotated controller beginning like this:

当我请求执行该网址的网址时控制器的代码,我的拦截器代码永远不会被调用。任何人都可以解释原因吗?

When I request a url that executes the controller's code, my interceptor code is never called. Can anyone please explain why?

拦截器代码是:

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

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class DomainNameInterceptor extends HandlerInterceptorAdapter {
    public boolean preHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler) 
         throws Exception {
    System.out.println("Why is this not called?");
    return true;
  }
}

我使用的是以下文档:
Spring Core 3.1.x文档

I was using the following documentation: Spring Core 3.1.x Documentation

我搜索了HandlerInterceptor,并按照附带链接文档中给出的示例进行了操作。

I did a search for HandlerInterceptor and followed the example given within the documentation in the included link.

推荐答案

如果您使用< mvc:annotation-driven /> 配置了MVC上下文,那么我认为在定义基于bean的bean时创建了handlerMapping此自定义命名空间将覆盖您已定义的handlerMapping。注册拦截器的更好方法是使用< mvc:interceptors> 子标签来定义拦截器,这样它就会被注册到正确的handlerMapping:

If you have configured your MVC context using <mvc:annotation-driven/>,then I think the handlerMapping created when defining beans based on this custom namespace is overriding the handlerMapping that you have defined. A better way to register your interceptors would be to use the <mvc:interceptors> subtag to define the interceptors, this way it will get registered to the correct handlerMapping:

<mvc:annotation-driven>
    <mvc:interceptors>
        <ref bean="interceptor"/>
    </mvc:interceptors>
</mvc:annotation-driven>

这篇关于Spring 3.1 HandlerInterceptor未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆