Validation with Type Conversion and Formatting in the
Sample Application
In the SpringBlog application, Spring 3's new type conversion and formatting system will be adopted.
For validation, the JSR-303 Bean Validation API will be used, with Hibernate Validator as the underlying
validation service provider.
To see how these techniques are adopted, let's take the major entity class, the AbstractBlogPosting
class, which is the base class for both blog posting entries and comments, as an example. The class
stores the common properties such as the subject, body, and post date, among others.
The main validation and formatting rules of the properties within the AbstractBlogPosting entity
class are as follows:
The subject field is mandatory, and the number of characters should be between
·
10 and 50.
The body field is mandatory, and the number of characters should be between 10
·
and 200.
The postDate field will be automatically populated by the application, and when it
·
is displayed to the frontend, we will use the format yyyy-MM-dd (the default date
pattern defined by ISO).
The creation date and last-modified date will be automatically populated by the
·
application, and when it is displayed to the frontend, we will use the format yyyy-
MM-dd'T'HH:mm:ss.SSSZZ (the default date-time pattern defined by ISO).
To define the validation and formatting rule in the AbstractBlogPosting class, standard JSR-303
annotations will be used and applied to the corresponding properties. Listing 14-30 shows the code
snippet of the AbstractBlogPosting class with validation and formatting annotations applied.
Listing 14-30. The AbstractBlogPosting Class
package com.apress.prospring3.springblog.domain;
import static javax.persistence.GenerationType.IDENTITY;
import java.io.Serializable;
import javax.persistence.*;
import javax.validation.constraints.*;
import
org.hibernate.annotations.Type;
import
org.joda.time.DateTime;
import
org.springframework.data.domain.Auditable;
import
org.springframework.format.annotation.DateTimeFormat;
import
org.springframework.format.annotation.DateTimeFormat.ISO;
@MappedSuperclass
public abstract class AbstractBlogPosting
implements BlogPosting, Auditable<String, Long>, Serializable {
// Other code omitted
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home