SPRING MVC 的 配置 包括 包扫描 视图解析器 文件上传解析器 拦截器等

来源:互联网 发布:js prompt不弹出窗口 编辑:程序博客网 时间:2024/04/29 18:02
<?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"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
    
    <!-- 启用spring mvc 注解-->
    <mvc:annotation-driven>
<!-- 启动JSON格式的配置 -->
    <mvc:message-converters>  
    <!-- 这里也可以自己定制class -->
       <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">  
           <property name="supportedMediaTypes">  
               <list>  
                   <value>text/html;charset=UTF-8</value>  <!-- 避免IE出现下载JSON文件的情况 -->
               </list>  
           </property>    
       </bean>  
   </mvc:message-converters>  
    </mvc:annotation-driven>
   
<!-- 对静态资源文件的访问   缓存一年
<mvc:resources mapping="/images/**" location="/WEB-INF/images/"  cache-period="31536000"/>
<mvc:resources mapping="/css/**" location="/WEB-INF/css/" />
<mvc:resources mapping="/js/**" location="/WEB-INF/js/" />
<mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/" />
<mvc:resources mapping="/favicon.ico" location="favicon.ico" />
-->  
 
<!-- 自动扫描的包名 ,使Spring支持自动检测组件,如注解的Controller-->
    <context:component-scan base-package="com.crm.controller" />
    <context:component-scan base-package="com.crm.service"/>
    

<!-- 视图解析器:定义跳转的文件的前后缀 -->  
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/jsp/" />  
        <property name="suffix" value=".jsp" />  <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑  -->
    </bean>  
    <!-- 文件上传解析器 -->  
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <property name="defaultEncoding" value="utf-8"></property>   
        <property name="maxUploadSize" value="10485760000"></property>  
        <property name="maxInMemorySize" value="40960"></property>  
   </bean>  
    <!--<mvc:view-controller path="/" view-name="forward:/index.jsp"/>  -->
    
    
    <!-- 缓存配置(两种) -->  
    <!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) -->  
    <cache:annotation-driven cache-manager="cacheManager"/>  
    <!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供的) -->  
    <!--   
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">  
        <property name="caches">  
            <set>  
                <bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>  
            </set>  
        </property>  
    </bean>  
     -->  
    <!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager即可 -->  
    <!-- Spring提供的基于的Ehcache实现的缓存管理器 -->  
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
        <property name="configLocation" value="classpath:ehcache.xml"/>  
    </bean>  
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">  
        <property name="cacheManager" ref="cacheManagerFactory"/>  
    </bean> 
    
<!--配置拦截器, 多个拦截器,顺序执行 --> 
<mvc:interceptors>  
<mvc:interceptor>  
<!-- 当设置多个拦截器时,先按顺序调用preHandle方法,然后逆序调用每个拦截器的postHandle和afterCompletion方法 -->
<!-- 
/**的意思是所有文件夹及里面的子文件夹
/*是所有文件夹,不含子文件夹
/是web项目的根目录 
exclude-mapping 不拦截的url
-->
<mvc:mapping path="/" />
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/code"/> 
<mvc:exclude-mapping path="/logout"/>  

<bean class="com.crm.interceptor.CommonInterceptor"></bean>  
</mvc:interceptor>
  <mvc:interceptor>
  <mvc:mapping path="/**"></mvc:mapping>
  <mvc:exclude-mapping path="/code"/> 
<mvc:exclude-mapping path="/logout"/>  
<mvc:exclude-mapping path="/login"/>  
  <bean class="com.crm.controller.MyInterceptor"></bean>
  </mvc:interceptor>
</mvc:interceptors>
      
</beans>   
0 0
原创粉丝点击