javablogspot

Just another WordPress.com weblog

JavaServer Faces JSF Validation Tutorial: Error Handling in JSF

Posted by damuchinni on March 2, 2009

JavaServer Faces technology supports a mechanism for validating the data of editable components. Each component in JavaServer Faces, created Error Messages during the life cycle of JSF and attached them to FacesContext object. Hence each message is attached to a component in the component tree and the message is displayed into the view at the end of JSF life cycle.
Showing Error Messages on View

JSF provides different tags to handle and display messages on the view. There are two message tags in SUN’s reference implementation of JSF HTML library:
view plaincopy to clipboardprint?

1.
2.

h:messages is used to display all messages at once. You can place h:messages tag in start of your form. You may need to display only global messages using h:messages tag. For displaying only global messages set globleOnly attribute of h:messages to true.
view plaincopy to clipboardprint?

1.

Use h:message to display message attached to one component. An attibute for=”” can be used to specify the id of a component whose error messages we need to display. h:message is used to display error message next to the component that generated the error. If more then one message is attached to that component, h:message will display the last message.
view plaincopy to clipboardprint?

1. …
2.
3.
4. …

Each message can have a summary description and a detailed description. When using the h:message tag, the default is to show the detail message. When using the h:messages tag, the default is to display the summary descriptions. To change the defaults, modify the boolean showSummary and showDetail attributes.
view plaincopy to clipboardprint?

1.
2.

You can also enable component’s detail message to appear as a tooltip. To do so, set tooltip attribute of message tag to true. Note that for enabling this option, showDetail and showSummary must be set to true.

There are four forms of JSF validation:
1. Built-in validation components
2. Application level validations
3. Custom validation components using Validator interface
4. Validation methods in backing beans
Built-in validation components

The SUN’s reference implementation of JSF provides some default validation components that can be leveraged to implement validation of any user inputs. The JSF’s core library provides tags to validate input. Following are few tags that can be used to validate the input.

f:validateDoubleRange : This tag checks the value of component within specified range. The value must be convertible to floating-point type or a floating-point itself.

f:validateLength : This tag checks the length of a value and restrict it within a specified range. The value must be of type java.lang.String.

f:validateLongRange : Checks is component value is within a specified range. The value must be of numeric type or string convertible to a long.
Example:
view plaincopy to clipboardprint?

1.
2.
3.
4. ….
5.
6.
7.

….

Validation using Backing Bean methods

A backing bean method can be used for doing validation of any input field. First we need to create the backing bean method that will get called during validation process. The signature of the method can be:
view plaincopy to clipboardprint?

1. public void (FacesContext context, UIComponent component, Object value) { .. }

public void (FacesContext context, UIComponent component, Object value) { .. }

Once a backing bean method is ready we can bind it with a component using f:validator tag.
view plaincopy to clipboardprint?

1.
2.

In above snippet, we have bind checkUsername() method of userBean to component inputText. It is possible to bind more than one validators to one component.

Backing bean method of validation is easy to implement, but this method is specific for one application and may not be reused for different application. To create generic validators which can be used in different application, Validator interface can be used.
Custom validation components using Validator interface

Validator interface can be extended and a custom validator can be created which can be reused across different applications in JSF. To create a custom validator, we need to create a Java class that implements javax.faces.validator.Validator interface. Validator interface provides a method validate () that needs to be implemented. Following is the signature of validate() method.
view plaincopy to clipboardprint?

1. import javax.faces.component.UIComponent;
2. import javax.faces.context.FacesContext;
3. import javax.faces.validator.Validator;
4. import javax.faces.validator.ValidatorException;
5. …
6. …
7. public void validate(FacesContext context, UIComponent component, Object value)
8. throws ValidatorException {
9. }

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;


public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
}

Once the Validator is implemented, we need to register this validator in faces-config.xml file. To do so copy following code in faces-config.xml assuming that our validator class name is net.viralpatel.jsf.helloworld.EmailValidator.
view plaincopy to clipboardprint?

1.
2. emailValidator
3. net.viralpatel.jsf.helloworld.EmailValidator
4.

emailValidator
net.viralpatel.jsf.helloworld.EmailValidator

We can bind this validator with any component using f:validator tag.
view plaincopy to clipboardprint?

1.
2.
3.

Note that in above code snippet, validatorId attribute of f:validator tag points to the validator’s ID that is registered in faces-config.xml file.
For validating email address we can create a Validator class as:
view plaincopy to clipboardprint?

1. package net.viralpatel.jsf.helloworld;
2.
3. import javax.faces.application.FacesMessage;
4. import javax.faces.component.UIComponent;
5. import javax.faces.context.FacesContext;
6. import javax.faces.validator.Validator;
7. import javax.faces.validator.ValidatorException;
8.
9. public class EmailValidator implements Validator{
10. public void validate(FacesContext context, UIComponent component, Object value)
11. throws ValidatorException {
12.
13. String email = (String) value;
14.
15. if(!email.contains(“@”)) {
16. FacesMessage message = new FacesMessage();
17. message.setSeverity(FacesMessage.SEVERITY_ERROR);
18. message.setSummary(“Email is not valid.”);
19. message.setDetail(“Email is not valid.”);
20. context.addMessage(“userForm:Email”, message);
21. throw new ValidatorException(message);
22. }
23. }
24. }

package net.viralpatel.jsf.helloworld;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class EmailValidator implements Validator{
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {

String email = (String) value;

if(!email.contains(“@”)) {
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_ERROR);
message.setSummary(“Email is not valid.”);
message.setDetail(“Email is not valid.”);
context.addMessage(“userForm:Email”, message);
throw new ValidatorException(message);
}
}
}

Thus by using JSF Validation framework, it is possible to validate user input easily. We saw different ways of validation in JSF: Default validators, backing bean methods and validation through validator interface.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: