您的位置:首页 > 编程语言 > Java开发

spring拦截器,HandlerInterceptorAdapter配置后未生效

2015-05-21 14:07 746 查看
编写了拦截器以后,提交请求,拦截器并没有生效,解决办法如下:

spring.xml文件,注意红色字体。

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"

 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/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

 

 <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->

 <context:component-scan base-package="com.*.controller" />

 

 <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/page/" p:suffix=".jsp" />

   

    <!-- 对静态资源文件的访问 --> 

    <mvc:resources mapping="/resource/**" location="/resource/" />

   

 <!-- 添加注解驱动 --> 

    <mvc:annotation-driven />

 

 <mvc:interceptors>

  <mvc:interceptor>

   <mvc:mapping path="/**" />

   <bean id="tttttttttt" class="com.BaseHandlerInterceptor" />

  </mvc:interceptor>

 </mvc:interceptors>


   

</beans>
 

BaseHandlerInterceptor代码如下:

public class BaseHandlerInterceptor extends HandlerInterceptorAdapter{

 @Override

 public boolean preHandle(HttpServletRequest request,

   HttpServletResponse response, Object handler) throws Exception {

  

  System.out.println("^^^^^^^^^^^^^^^^^^^^^^run preHandle ^^^^^^^^^^^^^^^^^^^^^^^^^");

  

  return true;

 }

 @Override

 public void postHandle(HttpServletRequest request,

   HttpServletResponse response, Object handler,

   ModelAndView modelAndView) throws Exception {

 }

 @Override

 public void afterCompletion(HttpServletRequest request,

   HttpServletResponse response, Object handler, Exception ex)

   throws Exception {

 }

}

 

BaseHandlerInterceptor拦截设置未生效是因为没有指定拦截模式,在spring.xml中指定拦截模式后,代码生效。

 
 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息