Validating input received from the user to maintain data integrity is an important part of application logic. Validation of data can take place at different layers in even the simplest of applications, as shown in the guessnumber example application from an earlier chapter. The guessnumber example application validates the user input (in the h:inputText tag) for numerical data at the presentation layer and for a valid range of numbers at the business layer.
JavaBeans Validation (Bean Validation) is a new validation model available as part of Java EE 6 platform. The Bean Validation model is supported by constraints in the form of annotations placed on a field, method, or class of a JavaBeans component, such as a backing bean.
Constraints can be built in or user defined. User-defined constraints are called custom constraints. Several built-in constraints are available in the javax.validation.constraints package. Table 9–2 lists all the built-in constraints.
Table 9–2 Built-In Bean Validation Constraints
In the following example, a constraint is placed on a field using the built-in @NotNull constraint:
public class Name { @NotNull private String firstname; @NotNull private String lastname; }
You can also place more than one constraint on a single JavaBeans component object. For example, you can place an additional constraint for size of field on the firstname and the lastname fields:
public class Name { @NotNull @Size(min=1, max=16) private String firstname; @NotNull @Size(min=1, max=16) private String lastname; }
The following example shows a method with a user-defined constraint that checks for a predefined email address pattern such as a corporate email account:
@ValidEmail public String getEmailAddress() { return emailAddress; }
For a built-in constraint, a default implementation is available. A user-defined or custom constraint needs a validation implementation. In the above example, the @ValidEmail custom constraint needs an implementation class.
Any validation failures are gracefully handled and can be displayed by the h:messages tag.
Any backing bean that contains Bean Validation annotations automatically gets validation constraints placed on the fields on a JavaServer Faces application’s web pages.
See Validating Persistent Fields and Properties for more information on using validation constraints.
The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all.
An empty string is represented as "". It is a character array of zero characters. A null string is represented by null. It can be described as the absence of a string instance.
Backing bean elements represented as a JavaServer Faces text component such as inputText are initialized with the value of the empty string by the JavaServer Faces implementation. Validating these strings can be an issue when user input for such fields is not required. Consider the following example, where the string testString is a bean variable that will be set using input typed by the user. In this case, the user input for the field is not required.
if (testString.equals(null)) { doSomething(); } else { doAnotherThing(); }
By default, the doAnotherThing method is called even when the user enters no data, because the testString element has been initialized with the value of an empty string.
In order for the Bean Validation model to work as intended, you must set the context parameter javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL to true in the web deployment descriptor file, web.xml:
<context-param> <param-name> javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL </param-name> <param-value>true</param-value> </context-param>
This parameter enables the JavaServer Faces implementation to treat empty strings as null.
Suppose, on the other hand, that you have a @NotNull constraint on an element, meaning that input is required. In this case, an empty string will pass this validation constraint. However, if you set the context parameter javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL to true, the value of the backing bean attribute is passed to the Bean Validation runtime as a null value, causing the @NotNull constraint to fail.