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中指定拦截模式后,代码生效。
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中指定拦截模式后,代码生效。
相关文章推荐
- SpringMVC HandlerInterceptorAdapter登陆验证拦截器
- springmvc拦截器HandlerInterceptorAdapter预处理
- Spring MVC拦截器HandlerInterceptorAdapter
- SpringMVC HandlerInterceptorAdapter
- SpringMvc Interceptor拦截器的配置与使用
- Spring MVC 中拦截器 HandlerInterceptorAdapter的使用
- springMvc拦截器(HandlerInterceptorAdapter)
- Spring MVC 中 HandlerInterceptorAdapter的使用(拦截器)
- Spring Boot WebMvcConfigurerAdapter 实现拦截器无配置
- Spring MVC HandlerInterceptorAdapter
- springBoot 使用继承WebMvcConfigurerAdapter 添加多个拦截器和继承HandlerInterceptorAdapter创建拦截器
- Spring MVC 中 HandlerInterceptorAdapter的使用[SpringMVC中请求拦截器的实现]
- Spring MVC中拦截器HandlerInterceptorAdapter中的preHandle方法
- 基于注解,HandlerInterceptorAdapter 拦截器
- SpringMVC配置拦截器出现500错误:Error creating bean with name 'org.springframework.web.servlet.handler.Mappe
- springMvc自定义拦截器HandlerInterceptorAdapter的认识
- SpringMVC拦截器(包括自定以拦截器--实现HandlerInterceptorAdapter)(资源和权限管理)
- SpringMvc拦截器HandlerInterceptorAdapter
- Spring MVC中的拦截器/过滤器HandlerInterceptorAdapter的使用
- spring HandlerInterceptorAdapter拦截ajax异步请求,报错ERR_INCOMPLETE_CHUNKED_ENCODING