Spring4.x官方参考文档中文版——第21章 Web MVC框架(32)

21.16.2 对提供的配置进行定制

        为了在Java中定制默认的配置,你只需实现WebMvcConfigurer接口,或者继承WebMvcConfigurerAdapter并覆写你需要的方法:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter{
 
    // 覆写配置的方法...
 
}

       为了定制<mvc:annotation-driven/>的默认配置,请确认它支持哪种attribute属性和子元素。你可以参考Spring MVC XML schema,或者使用你IDE里的代码自动补全功能来看看哪些attribute和子元素是可以用的。

 

21.16.3 转换与格式化

       默认情况下,Date和Number类型的格式化是已经配置好的,包括支持@NumberFormat和@dateTimeFormat注解。如果Joda Time在类路径下被发现,这个类型也能被格式化。你可以通过覆写addFormatters方法来注册自定义的formatter(格式化器)和converters(转换器)。

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
 
    @Override
    public void addFormatters(FormatterRegistry registry) {
        // 添加 formatters 和/或 converters
    }
 
}

        在MVC namespace中,当有<mvc:annotation-driven>时,相同的默认配置就会被启用了。如需自定义formatter和converter,只需添加ConversionService:

<?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 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>

提示:

   使用FormatterRegistrars时,可参考”8.6.4 FormatterRegistrar SPI”。

 

21.16.4 验证

       Spring提供了一个验证器接口,他能够适用于整个应用的任意层面上进行验证。在Spring MVC中,你可以把它作为一个全局Validator实例来进行配置,

可以在碰到任何@Valid 或@Validated的controller方法时使用它,并且/或者作为一个类中的Validator,通过@InitBinder方法来使用。全局或者本地的validator实例可以混合使用。

        Spring同样可以通过LocalValidatorFactoryBean来支持JSR-303/JSR-349的bean验证,LocalValidatorFactoryBean使Spring的org.springframework.validation.Validator接口适应于bean的验证javax.validation.validator规范。这个类可以作为全局验证器来被注入到Spring MVC中,这将会在后文进行说明。

        默认情况下,使用@EnableWebMvc或<mvc:annotaion_driven>会自动在Spring MVC中支持bean的验证,当诸如Hibernate Validator之类的验证器在类路径下被检测到的时候,会通过LocalValidatorFactoryBean来进行bean的验证。

 

提示:

        有时,在controller或其他类中注入LocalValidatorFactoryBean会比较方便。最简单的方法是,声明自己的@Bean,并且以@Primary标注它,用来避免与MVC Java config中的那个类似的起冲突。如果你更喜欢用MVC Java config中的那个,你需要在WebMvcConfigurationSupport中覆写mvcValidator方法,并显式地声明方法会返回LocalValidatorFactory而不是Validator。关于对这些已提供的配置进行切换扩展,请参考”21.16.13 使用MVC Java Config进行深度定制”。

 

        另一方面,你可以配置你自己的全局Validator实例:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
 
    @Override
    public Validator getValidator(); {
        // return "global" validator
    }
 
}

        在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
        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 validator="globalValidator"/>
 
</beans>

        为了把本地的验证与全局的验证结合起来,只需添加一个或多个验证器:

@Controller
public class MyController {
 
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.addValidators(new FooValidator());
    }
 
}

        使用这个最小化的配置,在任何时候,只要碰到了带@Valid或@Validated的方法入参,就会被已经配置好的验证器进行验证。任何违背验证规则的会在BindingResult自动地作为错误暴露出来,并且可以作为方法的入参被访问到,并且也可以在Spring MVC HTML的视图中进行渲染。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值