spring mvc 拦截器配置

来源:互联网 发布:细川忠兴 知乎 编辑:程序博客网 时间:2024/05/16 01:28
以前将filter和servlet都配置在web.xml中,有了spring mvc后,可以讲servlet配置为controller,将interceptor在spring mvc配置文件中xxx-servlet.xml中。
<mvc:interceptors>        <mvc:interceptor>            <mvc:mapping path="/user/detail.json"/>            <mvc:mapping path="/user/update.json"/>            <mvc:mapping path="/user/list.json"/>            <bean class="com.qunar.homework.web.interceptor.LoginInterceptor"/>        </mvc:interceptor>        <mvc:interceptor>            <mvc:mapping path="/user/*"/>            <bean class="com.qunar.homework.web.interceptor.RequestTimeInterceptor"/>        </mvc:interceptor>    </mvc:interceptors>

dispatcher-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: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.xsd       http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/spring-mvc.xsd       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context.xsd">    <mvc:resources mapping="/resources/**" location="/resources/" />    <mvc:interceptors>        <mvc:interceptor>            <mvc:mapping path="/user/detail.json"/>            <mvc:mapping path="/user/update.json"/>            <mvc:mapping path="/user/list.json"/>            <bean class="com.qunar.homework.web.interceptor.LoginInterceptor"/>        </mvc:interceptor>        <mvc:interceptor>            <mvc:mapping path="/user/*"/>            <bean class="com.qunar.homework.web.interceptor.RequestTimeInterceptor"/>        </mvc:interceptor>    </mvc:interceptors>    <!--启用 Spring 的注解 load 方式-->    <context:annotation-config />    <!--将所有的 controller 作为 Bean 注册到Spring的应用上下文中-->    <context:component-scan base-package="qunar.web.spring, com.qunar.homework.web.controller" />    <!--<mvc:annotation-driven />-->    <!--这里通过扫描 qunar.web.spring 包下面的 WebConfig,启用 @JsonBody注解-->     <!--如果使用 @JsonBody,注意一下-->     <!--1. 不能使用短标签 mvc:annotation-driven-->     <!--2. 不需要自己注册 HandlerAdapter-->    <!-- 添加视图解析器,并为逻辑视图加上前缀和后缀,查找视图模板,生成最终的解析视图 xxx.jsp-->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />        <property name="prefix" value="/WEB-INF/views/" />        <property name="suffix" value=".jsp" />        <property name="order" value="1" />    </bean>    <!--<!– 注册HandlerMapping,这里包含了所有url到controller的映射关系 –>-->    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">-->        <!--<property name="interceptors">-->            <!--<list>-->                <!--<bean class="com.qunar.homework.web.interceptor.RequestTimeInterceptor" />-->            <!--</list>-->        <!--</property>-->    <!--</bean>-->    <mvc:default-servlet-handler /></beans>


0 0
原创粉丝点击