Validation, Data Binding, and Type Conversion

将验证视为业务逻辑既有优势也有劣势,Spring 提供了一种验证和数据绑定的设计,并不排斥两者中的任何一个。具体来说,验证不应该绑定到 Web 层,而且应该易于本地化,并且应该可以插入任何可用的验证器。考虑到这些问题,Spring 提供了一个 Validator 合约,它既是基本的,又是完全可以用于应用程序各个层的。

There are pros and cons for considering validation as business logic, and Spring offers a design for validation and data binding that does not exclude either one of them. Specifically, validation should not be tied to the web tier and should be easy to localize, and it should be possible to plug in any available validator. Considering these concerns, Spring provides a Validator contract that is both basic and eminently usable in every layer of an application.

数据绑定对于让用户输入动态绑定到应用程序的域模型(或您用来处理用户输入的任何对象)来说非常有用。Spring 提供了恰如其分地命名的 DataBinder 来执行此操作。ValidatorDataBinder 构成了 validation 包,主要用于 Web 层,但不限于此。

Data binding is useful for letting user input be dynamically bound to the domain model of an application (or whatever objects you use to process user input). Spring provides the aptly named DataBinder to do exactly that. The Validator and the DataBinder make up the validation package, which is primarily used in but not limited to the web layer.

BeanWrapper 是 Spring Framework 中的一个基本概念,在诸多地方都有使用。然而,您可能不需要直接使用 BeanWrapper。不过,由于这是参考文档,我们认为有必要做一些解释。我们将在本章中介绍 BeanWrapper,因为如果您打算使用它,那么您很可能是在尝试将数据绑定到对象时使用它。

The BeanWrapper is a fundamental concept in the Spring Framework and is used in a lot of places. However, you probably do not need to use the BeanWrapper directly. Because this is reference documentation, however, we feel that some explanation might be in order. We explain the BeanWrapper in this chapter, since, if you are going to use it at all, you are most likely do so when trying to bind data to objects.

Spring 的 DataBinder 和较低级别的 BeanWrapper 都使用 PropertyEditorSupport 实现来解析和格式化属性值。PropertyEditorPropertyEditorSupport 类型是 JavaBeans 规范的一部分,并且在此章节中也有说明。Spring 的 core.convert 包提供了一般类型转换工具,以及用于格式化 UI 域值的更高级别的 format 包。你可以将这些包用作 PropertyEditorSupport 实现的更简单的替代方法。它们也在这一章中进行了讨论。

Spring’s DataBinder and the lower-level BeanWrapper both use PropertyEditorSupport implementations to parse and format property values. The PropertyEditor and PropertyEditorSupport types are part of the JavaBeans specification and are also explained in this chapter. Spring’s core.convert package provides a general type conversion facility, as well as a higher-level format package for formatting UI field values. You can use these packages as simpler alternatives to PropertyEditorSupport implementations. They are also discussed in this chapter.

Spring 通过设置基础设施和 Spring 自己的 "@8" 合约适配器支持 Java Bean 验证。应用程序可以一次全局启用 Bean 验证,如 "@12" 中所述,并且可以仅用于所有验证需要。在 Web 层中,应用程序可以针对每个 "@10" 注册控制器本地的 Spring "@9" 实例,如 "@13" 中所述,这有助于引入自定义验证逻辑。

Spring supports Java Bean Validation through setup infrastructure and an adaptor to Spring’s own Validator contract. Applications can enable Bean Validation once globally, as described in Java Bean Validation, and use it exclusively for all validation needs. In the web layer, applications can further register controller-local Spring Validator instances per DataBinder, as described in Configuring a DataBinder, which can be useful for plugging in custom validation logic.