JSF Standard Validators Example
Hey geeks, today we ‘re gonna talk about JSF Standard Validators.
Just like any other framework, JSF is here to help us save time from common development tasks, such as form validations. We can obviously write our own, custom validators for our site’s forms, but there are some standard validators provided from JSF that handle checks for string lengths and numeric ranges.
To begin with, suppose a sample form, where the user is prompted to submit his username, age and salary.
1. Project Environment
This example was implemented using the following tools:
- JSF 2.2
- Maven 3.1
- Eclipse 4.3 (Kepler)
- JDK 1.7
- Apache Tomcat 7.0.41
Just like any other of my previous JSF examples, you need to create a Dynamic Web Project with Maven and JSF should be included in it. At any case, if you don’t remember some configurations, consult my very first example according to JSF.
This is the project’s final structure, just to ensure that you won’t get lost anytime.
2. JSF Pages
As I fore-mentioned, we ‘ll here use three form fields: username, age and salary. A typical validation process could be:
- Username should be 5-12 (five to twelve) characters long.
- Age should be between 12 and 110 years.
- Salary should be between 653.90 and 3500.1 .
We have different validation criteria in each one of our form fields, so each validation has to be done inside the h:inputText
tag, according to our constraints.
index.xhtml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" > <h:head> <title>JSF Standard Validators Example</title> </h:head> <h:body> <h1>JSF 2.0 Standard Validators Example</h1> <h:form> <h:panelGrid columns="3"> Username: <h:inputText id="username" value="#{user.username}" size="15" required="true" label="Username"> <f:validateLength minimum="5" maximum="12" /> </h:inputText> <h:message for="username" style="color:red" /> Age: <h:inputText id="age" value="#{user.age}" size="2" required="true" label="Age"> <f:validateLongRange minimum="12" maximum="110" /> </h:inputText> <h:message for="age" style="color:red" /> Salary: <h:inputText id="salary" value="#{user.salary}" size="7" required="true" label="Salary"> <f:validateDoubleRange minimum="653.90" maximum="3500.1" /> </h:inputText> <h:message for="salary" style="color:red" /> </h:panelGrid> <h:commandButton value="Submit" action="response"/> </h:form> </h:body> </html>
The f:validateLength
tag is used to verify that a component’s length is within a specified range, which in our case, is translated between 5 and 12. It is used for String types.
The f:validateLongRange
tag is used to verify that a component’s value is within a specified range, which in our case, is translated between 12 and 110. It supports any numeric type or String that can be converted to long.
The f:validateDoubleRange
tag is used to verify that a component’s value is within a specified range, which in our case, is translated between 653.90 and 3500.1. It supports any value that can be converted to floating-point type or floating point.
Just for demonstration purposes, we are creating a response page, to show the success scenario of the validation.
response.xhtml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>JSF Standard Validators Example</title> </h:head> <h:body> <h1>JSF 2.0 Standard Validators Example - Response Page</h1> Your username is : <h:outputText value="#{user.username}" /> <br/>Your salary is : <h:outputText value="#{user.salary}" /> <br/>Your age is: <h:outputText value="#{user.age}" /> </h:body> </html>
3. Managed Bean
Our JSF pages interact with each other because of a ManagedBean, so here it is:
ManagedBean.java
package com.javacodegeeks.enterprise.jsf.standardvalidators; import java.io.Serializable; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean @SessionScoped public class UserBean implements Serializable{ private static final long serialVersionUID = 7134492943336358840L; private double salary; private String username; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }
4. Demo
As a first example and just to ensure our validators’ proper functionality, I am providing invalid data to all fields:
Well, it seems to work properly, so let’s go for some valid values:
5. Download the Eclipse Project
This was an example of JSF Standard Validators.
You can download the full source code of this example here : JSFStandardValidators.zip