Spring 5.2.2 MVC(22)—MVC配置之啓用MVC、type轉換、MVC配置API

      MVC Java配置和MVC XML命名空間提供了適合大多數應用程序的默認配置,並提供了一個配置API來定製它。你不須要了解由MVC Java配置和MVC命名空間建立的底層bean。若是你想了解,請看以前發的內容。java

一、啓用MVC配置spring

     在Java配置中,可使用@EnableWebMvc註釋來啓用MVC配置,以下例所示:spring-mvc

@Configuration@EnableWebMvcpublic class WebConfig {}

在XML配置中,可使用<mvc:annotation-driven>啓用MVC配置,以下例所示:微信

<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/>
</beans>

    示例中註冊了許多Spring MVC基礎bean,並根據類路徑上可用的依賴性進行調整(例如,JSON、XML和其餘類型的負載轉換器)。mvc

二、MVC配置APIapp

      在Java配置中,能夠實現WebMvcConfigurer 接口,以下例所示:ide

@Configuration@EnableWebMvcpublic class WebConfig implements WebMvcConfigurer {
// 實現配置方法。。。}

     在XML中,能夠檢查<mvc:annotation-driven/>. 你能夠查看Spring MVC XML模式,或者使用IDE的代碼完成特性來發現哪些屬性和子元素是可用的。spa

三、type轉換.net

    默認狀況下有了 NumberDate 類型的格式化程序,包括對@NumberFormat@DateTimeFormat註解的支持。code

    在Java配置中,能夠註冊自定義格式化程序和轉換器,以下例所示:

@Configuration@EnableWebMvcpublic class WebConfig implements WebMvcConfigurer {
@Override public void addFormatters(FormatterRegistry registry) { // ... }}

下面的示例演示如何在XML中實現相同的配置:

<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="org.example.MyConverter"/> </set> </property> <property name="formatters"> <set> <bean class="org.example.MyFormatter"/> <bean class="org.example.MyAnnotationFormatterFactory"/> </set> </property> <property name="formatterRegistrars"> <set> <bean class="org.example.MyFormatterRegistrar"/> </set> </property> </bean>
</beans>

FormatterRegistrar 是一個SPI,用於經過FormatterRegistry註冊格式化程序和轉換器。下面的列表顯示了其接口定義:

package org.springframework.format;
public interface FormatterRegistrar {
void registerFormatters(FormatterRegistry registry);}

當爲格式類別(如日期格式)註冊多個相關的轉換器和格式設置程序時,FormatterRegistrar 很是有用。在聲明性註冊不足的狀況下(例如,當格式化程序須要在與其本身的<T>不一樣的特定字段類型下編制索引時,或在註冊打印機/分析器對時),它也頗有用。

    默認狀況下,日期和時間是沒有被@DateTimeFormat註解的,而是使用DateFormat.SHORT 風格。若是願意,能夠經過定義本身的全局格式來更改此設置。

    爲此,須要確保Spring不註冊默認格式化程序。相反應該手動註冊全部格式化程序。使用org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar或者org.springframework.format.datetime.DateFormatterRegistrar類,這取決因而否使用Joda時間庫。

     例如,如下Java配置註冊全局yyyyMMdd格式(此示例不依賴於Joda時間庫):     

@Configurationpublic class AppConfig {
@Bean public FormattingConversionService conversionService() {
// 使用DefaultFormattingConversionService,但不註冊默認值 DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false);
        // 確保支持@NumberFormat conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
// 使用特定全局格式進行註冊日期轉換 DateFormatterRegistrar registrar = new DateFormatterRegistrar(); registrar.setFormatter(new DateFormatter("yyyyMMdd")); registrar.registerFormatters(conversionService);
return conversionService; }}

    若是喜歡基於XML的配置,那麼可使用FormattingConversionServiceFactoryBean。下面的示例演示瞭如何執行此操做(此次使用Joda-time):

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd>
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="registerDefaultFormatters" value="false" /> <property name="formatters"> <set> <bean class="org.springframework.format.number.NumberFormatAnnotationFormatterFactory" /> </set> </property> <property name="formatterRegistrars"> <set> <bean class="org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar"> <property name="dateFormatter"> <bean class="org.springframework.format.datetime.joda.DateTimeFormatterFactoryBean"> <property name="pattern" value="yyyyMMdd"/> </bean> </property> </bean> </set> </property> </bean></beans>

    Joda-Time提供了不一樣的類型來表示datetimedate-time值。應該使用JodaTimeFormatterRegistrar dateFormattertimeFormatterdateTimeFormatter 屬性爲每種類型配置不一樣的格式。DateTimeFormatterFactoryBean 提供了一種建立格式化程序的方便方法。

    若是您使用Spring MVC,請記住配置所使用的轉換服務。對於基於Java的@Configuration,這意味着擴展WebMvcConfigurationSupport 類並重寫mvcConversionService()方法。對於XML,應該使用mvc:annotation-driven的元素的conversion-service屬性。


   今天MVC配置開了一個頭,明天繼續講MVC配置--Validation、Interceptors等。

敬請持續關注。


歡迎關注和轉發Spring中文社區(加微信羣,能夠關注後加我微信):


本文分享自微信公衆號 - Spring中文社區(gh_81d233bb13a4)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索