springmvc配置詳解

約束文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">
</beans>

開啓註解掃描

就是spring的註解掃描開啓
base-package:要掃描的包

<context:component-scan base-package="****"/>

配置視圖解析器

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix" value="/WEB-INF/pages/"/>
   <property name="suffix" value=".jsp"/>
</bean>

放開靜態資源的攔截

靜態資源位置webapp目錄下和WEB-INF目錄同級參考以下配置

<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>

spring開啓註解mvc的支持

<mvc:annotation-driven/>

自定義類型轉換器

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <set>
        	<!--自定義類型轉換器類名-->
            <bean class="cn.td.utils.StringDateConverter"/>
        </set>
    </property>
</bean>

在spring開啓註解mvc的支持的後面配置自定義類型轉換器 conversion-service:

<mvc:annotation-driven conversion-service="conversionService"/>

文件解析器

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	<!--上傳的文件最大爲10485760byte-->
    <property name="maxUploadSize" value="10485760"/>
</bean>

異常處理器

 <!--異常處理器-->
<bean id="sysExceptionResolver" class="cn.td.exception.SysExceptionResolver"/>

攔截器

<!--配置攔截器-->
    <mvc:interceptors>
        <!--配置攔截器-->
        <mvc:interceptor>
            <!--要攔截的具體方法 path="/user/*" 表示攔截user下的所有方法-->
            <mvc:mapping path="/user/*"/>
            <!--不要攔截的具體方法
            <mvc:exclude-mapping path=""/>
            -->
            <!--配置攔截器對象-->
            <bean id="myInterceptor" class="cn.td.interceptor.MyInterceptor1"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章