原因在于当你使用:[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]
基于注解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条回答
为你推荐
- ssm 框架,在本地环境下完美运行,在服务器上定时器执行两次
- java
- spring
- 2个回答
- ssm 报错:org.springframework.context.annotation.internalAsyncAnnotationProcessor
- centos
- java
- spring
- 缓存
- 2个回答
- Java ssm 框架 访问报错500
- spring
- tomcat
- 4个回答
- springmvc防止表单重复提交的一个问题不是很理解
- spring
- 3个回答
- MavenWeb项目使用mybatis的分页查询插件,在项目部署时出现错误
- java
- maven
- spring
- github
- tomcat
- 2个回答
- SSM整合运行Tomcat报错org.springframework.beans.factory.BeanCreationException
- java
- java-ee
- spring
- jar
- intellij-idea
- 2个回答
- spring mvc的奇怪问题,关于注解的路径问题
- spring
- 0个回答
- hibernate 不用事务,也提交到了数据库,帮忙看看
- hibernate
- spring
- 0个回答
- springMVC spring3.1 hibernate4 cxf整合发布restful风格的webservice
- 编程语言问答
- 互联网问答
- it技术
- IT行业问题
- 计算机技术
- 0个回答
- springMVC+ehcache 配置没有问题,但是缓存不起作用
- 编程语言问答
- 互联网问答
- it技术
- IT行业问题
- 计算机技术
- 0个回答
- ehcache+springAOP配置不成功?
- 编程语言问答
- 互联网问答
- it技术
- IT行业问题
- 计算机技术
- 0个回答
- 求各位大哥们指教一下,关天SPRINGMVC3.0和hibernate3.3整合
- hibernate
- spring mvc
- 0个回答
- 基于注解SpringMvc拦截器无法使用
- spring
- 0个回答
- JQuery ajax提交请求到一个Controller方法,这个方法不存但是运行没错是怎么回事?
- java-ee
- spring
- 2个回答
- shiro的 @RequiresPermissions 没作用 帮我看看是什么地方出了问题
- shiro
- 编码
- 异常
- redirect
- 6个回答
- ssm 整合 service层使用Autowired注入dao 报红,可以使用,为什么?
- ssh框架搭建
- 依赖注入
- 7个回答
- 整了一晚上SpringMVC,就是出不来!
- 3个回答
- ssh中怎么整合shiro,请大神指点
- shiro
- struts
- shiro 登录拦截
- spring
- ssh shiro
- 0个回答