基于注解SpringMvc拦截器无法使用

在我自定义的拦截器中打了断点一直无法进入,拦截器定义在controllers.xml中
代码如下帮忙解决下,谢谢
[code="java"]
package com.core.Interceptor;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
/**

  • 拦截器
    */
    public class BasedInterceptor extends HandlerInterceptorAdapter {
    //预处理
    public boolean preHandle(HttpServletRequest request,
    HttpServletResponse response, Object handler) throws Exception {
    HttpSession sesson = request.getSession();
    if(sesson.getAttribute("user") == null){
    request.getRequestDispatcher("index.jsp").forward(request, response);
    return false;
    }
    return true;
    }

    public void postHandle(HttpServletRequest request,
    HttpServletResponse response, Object handler,
    ModelAndView modelAndView) throws Exception {
    request.getRequestDispatcher("index.jsp").forward(request, response);
    }

    public void afterCompletion(HttpServletRequest request,
    HttpServletResponse response, Object handler, Exception ex)
    throws Exception {
    request.getRequestDispatcher("index.jsp").forward(request, response);
    }
    }
    [/code]
    web.xml
    [code="xml"]
    <?xml version="1.0" encoding="UTF-8"?>

    SpringTest


contextConfigLocation
classpath:applicationContext*.xml


org.springframework.web.context.ContextLoaderListener

<!-- 对像js,css,gif等静态文件的访问,需要映射到默认的servlet -->
<!-- 这里省去对静态资源url的配置,当然这样的话tomcat控制台就会报警告了,对html的请求、页面中的图片及css效果也是无法访问的
不过我们这是搭基本环境嘛,就先不要图片吧,如果想解决这个问题,可以看我另外一篇文章:
-->
<!-- 配置spring核心servlet -->

spring
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
/WEB-INF/servlet-context.xml

1


spring
.do
<!-- url配置为/,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为.do,则不影响静态文件的访问 -->

<!-- Spring 刷新Introspector防止内存泄露 -->





org.springframework.web.util.IntrospectorCleanupListener



<!-- 字符编码过滤器 -->  
<filter>  
    <filter-name>encodingFilter</filter-name>  
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    <init-param>  
        <param-name>encoding</param-name>  
        <param-value>GBK</param-value>  
    </init-param>  
</filter>  

<filter-mapping>
    <filter-name>encodingFilter</filter-name>  
    <url-pattern>*.do</url-pattern>  
</filter-mapping>

  <!-- 欢迎页面 -->  
<welcome-file-list>  
    <welcome-file>index.jsp</welcome-file>  
</welcome-file-list>  


[/code]
servlet-context.xml
[code="xml"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

<!-- Configures the @Controller programming model 必须加上这个,不然请求controller时会出现no mapping url错误-->
<mvc:annotation-driven />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/jsp directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/view/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>


<beans:bean id="multipartResolver"     
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver"     
      p:defaultEncoding="utf-8" />

<!-- Imports user-defined @Controller beans that process client requests -->
<beans:import resource="controllers.xml" />

/beans:beans
[/code]
controllers.xml
[code="xml"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" default-autowire="byName">

<!-- Maps '/' requests to the 'home' view 
    <mvc:view-controller path="*.do" view-name="home"/>-->

<!-- enable autowire -->
<context:annotation-config />

<!--拦截器-->
<bean id="basedAccessInterceptor" class="com.core.Interceptor.BasedInterceptor"/>  
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">  
    <property name="interceptors">  
        <list>  
            <ref bean="basedAccessInterceptor"/>  
        </list>  
    </property>  
</bean>  

<!-- 扫描业务组件 -->
<context:component-scan base-package="com.mvc.controller" />


[/code]

2个回答

原因在于当你使用:[code="java"] [/code]时,其实它已经注册了一个DefaultAnnotationHandlerMapping ,而后面你自己注册的
[code="java"]














[/code]
优先级没它内部的高,所以一直都不会调用你注册的拦截器。你可以通过:[code="java"] mvc:interceptors[/code]标签单独添加拦截器。spring论坛也有讨论:
[url]http://forum.springsource.org/showthread.php?81238-Conflict-between-lt-mvc-annotation-driven-gt-and-DefaultAnnotationHandlerMapping[/url]
参考:
[url]http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html[/url]中[b]15.12 Configuring Spring MVC[/b]

拦截器要拦截的URL是什么没有写,比如:

[code="xml"]


[/code]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!