配置Spring MVC
web
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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"> <mvc:annotation-driven /> </beans>
以上註冊了一個RequestMappingHandlerMapping,一個RequestMappingHandlerAdapter和一個ExceptionHandlerExceptionResolver支持使用註解配置控制器方法。spring
自定義provided配置spring-mvc
<mvc:annotation-driven conversion-service="conversionService"> <mvc:message-converters> <bean class="org.example.MyHttpMessageConverter"/> <bean class="org.example.MyOtherHttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBea <property name="formatters"> <list> <bean class="org.example.MyFormatter"/> <bean class="org.example.MyOtherFormatter"/> </list> </property> </bean>
配置攔截器
能夠配置HandlerInterceptors或WebRequestIinterceptors應用與全部進來的請求或受限的具體URL。
使用<mvc:interceptors>元素mvc
<mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> <mvc:interceptor> <mapping path="/**"/> <exclude-mapping path="/admin/**"/> <bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/> </mvc:interceptor> <mvc:interceptor> <mapping path="/secure/*"/> <bean class="org.example.SecurityInterceptor"/> </mvc:interceptor> </mvc:interceptors>
這是定義一個ParameterizableViewController緊隨轉發到一個視圖調用時的快捷方式。
app
配置視圖控制器,使用<mvc:view-controller>元素ide
<mvc:view-controller path="/" view-name="home"/>
配置靜態資源spa
<mvc:resources mapping="/resources/**" location="/public-resources/"/> <mvc:resources mapping="/resources/**" location="/public-resources/" cacheperiod="31556926"/> <mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/public-webresources/"/>