Topic 14
■■■
Validation with Type Conversion
and Formatting
In an enterprise application, validation is critical. The purpose of validation is to verify that the data
being processed fulfills all predefined business requirements as well as to ensure the data integrity and
usefulness in other layers of the application.
In application development, data validation is always mentioned along with conversion and
formatting. The reason is that most likely the format of the source of data is different from the format
being used in the application server. For example, in a web application, a user enters information in the
web browser frontend. When the user saves the data, the data is sent to the server (after the local
validation has completed). On the server side, a data-binding process will be performed, in which the data
from the HTTP request will be extracted, converted, and bound to the corresponding domain objects (for
example, users enter contact information in an HTML form that will be bound to a Contact object in the
server), based on the formatting rules defined for each attribute (for example, the date format pattern is
yyyy-MM-dd). When the data binding is complete, the validation rules are applied to the domain object to
check for any constraint violation. If everything runs fine, the data is persisted, and a success message is
displayed to the user. Otherwise, validation error messages are populated and displayed to the user.
In the first part of this chapter, you will learn how Spring provides sophisticated support for type
conversion, field formatting, and validation. Specifically, this chapter will cover the following topics:
The Spring type conversion system and the formatter service provider interface
·
(SPI): We will discuss the new generic type conversion system and formatter SPI
introduced in Spring 3. We will show you how the new services can be used to
replace the previous PropertyEditor support and how they convert between any
Java types.
Validation in Spring: We will discuss how Spring supports domain object
·
validation. First, we will provide a short introduction to Spring's own Validator
interface. Then, we will focus on the JSR-303 Bean Validation support offered
since Spring 3.
Creating a Project in STS for Samples
Let's create the project for the samples in this chapter. In STS, create a new Spring template project and
choose Simple Spring Utility Project.
In addition, upon project creation, add the other required dependencies , as shown in Table 14-1.
Also, verify that the project is using Spring 3.1 and JDK 6.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home