@NotNull
@Size(min=10, max=2000, message="{validation.posting.body.Size.message}")
@Column(name = "BODY")
public String getBody() {
return body;
}
@Column(name = "POST_DATE")
@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
@DateTimeFormat(iso=ISO.DATE)
public DateTime getPostDate() {
return postDate;
}
@NotNull
@Size(min=10, max=50, message="{validation.posting.subject.Size.message}")
@Column(name = "SUBJECT")
public String getSubject() {
return subject;
}
@Column(name = "CREATED_DATE")
@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
@DateTimeFormat(iso=ISO.DATE_TIME)
public DateTime getCreatedDate() {
return this.createdDate;
}
@Column(name = "LAST_MODIFIED_DATE")
@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
@DateTimeFormat(iso=ISO.DATE_TIME)
public DateTime getLastModifiedDate() {
return this.lastModifiedDate;
}
}
In Listing 14-30, you can see that the @NotNull and @Size JSR-303 annotations are applied to both
the body and subject properties. For the @Size annotation, instead of hard-coding the message text into
the message attribute, we provided the key of the message, and Hibernate Validator will retrieve the
corresponding message template text from the message resource bundle (the default name of the
message bundle properties file is ValidationMessages.properties), so as to support i18n
(internationalization). Listing 14-31 shows the two messages defined in Listing 14-30.
Listing 14-31. The ValidationMessages.properties File
validation.posting.body.Size.message=body must be between {min} and {max} characters
validation.posting.subject.Size.message=subject must be between {min} and {max} characters
In Listing 14-31, you can see that the variables min and max are defined in the message template,
which will be automatically substituted with the corresponding attributes within the @Size annotation.
By defining the message this way, i18n can be implemented easily. For example, to support Hong Kong's
traditional Chinese (with locale code zh_HK), we just need to prepare a
ValidationMessages_zh_HK.properties file with the messages in traditional Chinese.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home