xyb1988825
2011-08-23 13:32
浏览 533
已采纳

基于注解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条回答 默认 最新

相关推荐 更多相似问题