SPRING MVC 的 配置 包括 包掃描 視圖解析器 文件上傳解析器 攔截器等

<?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>   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章