首页 » 讨论区

spring mvc拦截器配置失败,求指点

hunterli 发布于 2012年01月22日 发表回帖

最近在学习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>

有2个回帖,1 - 2
hunterli 01-22 09:44

看来大家都忙着过年那,自己顶一顶。

超级奶爸老谭! 01-27 14:07

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

改为 <mvc:mapping path="/**"/> 试一下

登录后可以发表评论,现在登录

相关话题
. Spring MVC的验证
. Spring mvc 多视图链表
. 一个关于spring MVC的小问题
. 关于以Spring3.0 MVC进行项目开发的疑问...
. spring3 mvc 通配符配置