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源创计划”,欢迎正在阅读的你也加入,一块儿分享。

相关文章
相关标签/搜索