最近在学习springMvc,现在看到自定义拦截器这一章节,按照文档以及网上样例配置后,一直拦截不到请求,大家帮忙看下是怎么回事,不胜感激。我的配置及代码: cactus-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:annotation-config/>
<context:component-scan base-package="cactus"/>
<mvc:annotation-driven/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<bean class="cactus.fw.mvc.CharacterEncodingInterceptor">
<property name="ajaxMark">
<value>ajax</value>
</property>
<property name="normalEncoding">
<value>gbk,gbk</value>
</property>
<property name="ajaxEncoding">
<value>utf-8,utf-8</value>
</property>
</bean>
</mvc:interceptor>
</mvc:interceptors>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value=""/>
<property name="suffix" value=""/>
</bean>
</beans>
CharacterEncodingInterceptor.java:package cactus.fw.mvc;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class CharacterEncodingInterceptor extends HandlerInterceptorAdapter {
private String ajaxMark = null;
private String normalEncoding = null;
private String ajaxEncoding = null;
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
System.out.println("I am here.");
String[] encodings = null;
if(request.getHeader(ajaxMark) == null) {
encodings = normalEncoding.split(",");
} else {
encodings = ajaxEncoding.split(",");
}
request.setCharacterEncoding(encodings[0]);
response.setCharacterEncoding(encodings[1]);
return true;
}
public String getAjaxMark() {
return ajaxMark;
}
public void setAjaxMark(String ajaxMark) {
this.ajaxMark = ajaxMark;
}
public String getNormalEncoding() {
return normalEncoding;
}
public void setNormalEncoding(String normalEncoding) {
this.normalEncoding = normalEncoding;
}
public String getAjaxEncoding() {
return ajaxEncoding;
}
public void setAjaxEncoding(String ajaxEncoding) {
this.ajaxEncoding = ajaxEncoding;
}
}
奇怪的是,我把拦截器配置那一块改成下面的样子,就可以拦截到请求:<mvc:interceptors> <bean class="cactus.fw.mvc.CharacterEncodingInterceptor"> <property name="ajaxMark"> <value>ajax</value> </property> <property name="normalEncoding"> <value>gbk,gbk</value> </property> <property name="ajaxEncoding"> <value>utf-8,utf-8</value> </property> </bean> </mvc:interceptors>
看来大家都忙着过年那,自己顶一顶。######<mvc:mapping path=
"/*"
/>
改为
<mvc:mapping path=
"/**"
/> 试一下
######回复 @BlackApple : 表示该路径下的资源,不包含子目录的内容,表示该目录下的资源,包含子目录(递归)。######一个 和两个 有什么区别呀.不太明白, 不是代表一个或多个字符吗?######汗,查到了:匹配0或者更多的目录。 http://blog.csdn.net/li_jinjian2005/article/details/2915446######谢谢回复,改成/**之后好使了,为什么需要多加一个星号那?两者有什么区别?######extends HandlerInterceptorAdapter 换成 implements HandlerInterceptor
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。