JSF 2 f validator

JSF 2 f validator


Posted in : Java Posted on : June 20, 2012 at 6:52 PM Comments : [ 0 ]

In this tutorial you will learn about the JSF 2 f validator tag.

JSF 2 f validator

In this tutorial you will learn about the JSF 2 f validator tag.

To use the custom (user defined) validators on the JSF pages the <f:validator> tag of JSF 2 core tag library is used. These custom validators are the validators which are not supported by the JSF. An UIComponent that you want to validate by yourself defined validator this tag must be used. Within which UIComponent this tag is used it registers the instance of a named Validator and if this UIComponent tag has the UIComponent children then the defined validator will be applied along with this UIComponent to all the child component automatically.

Attributes of <f:validator>

  • disabled : This attribute is not required to use with this tag. This attribute evaluates to java.lang.Boolean value of which specifies whether this validator will be enabled for the nested tag or not.
  • validatorId : This attribute is not required to use with this tag. This attribute specifies is a Validator identifier evaluates to java.lang.String. This value should be either registered by @FacessValidator("fully classified name of validatiorId") or in the faces-config.xml as :
    <validator>
    <validator-id>validatorId</validator-id>
    <validator-class>fullyClassifiedNameofValidatorClass</validator-class>
    </validator>
  • binding : This attribute is not required to be used with this tag but, is used to bound the expression to evaluates the javax.faces.validator.Validator implemented object.

  • for : This is not a required attribute to be used with this tag. This attribute is used to refer one of the composite component's value within which this tag is nested.

Example :

Here an example is being given for you which will demonstrate about how to use the JSF 2 f validator tag. As earlier in this tutorial we learnt that this tag is used for the user custom Validators. So, before the making of example lets know the steps we required to do for using the <f:validator> tag.

  • Create a Validator class i.e a class which implements the javax.faces.validator.Validator interface with its method validate().
  • Then assign an unique validatorId as discussed above in the validatorId attribute.
  • Then use the <f:validator> tag with validatorId attribute in your JSF page.

NOTE : You may create your JavaBeans class seperately.

As discussed above I have created a JavaBeans class named NumberBean.java with the property num and its setter getter method. Then created a Validator class by named NumberValidatorBean and assigned a validatorId using the @FacesValidator(). In the validate() method wrote the code for validating the range of number for an input textbox and the corresponding error message if the input number is not in the range. Then created JSF pages for taking the input of number and used the <f:validator> tag inside the <h:inputText> tag to apply the validation on the input textbox. And the other JSF page is for the output if the input number is in the specified range.

Directory Structure

NumberBean.java

package devmanuals;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name="numBean")
@RequestScoped
public class NumberBean {

private int num;

public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
} 
}

NumberValidatorBean.java

package devmanuals;

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

@FacesValidator("devmanuals.NumberValidatorBean")
public class NumberValidatorBean implements Validator{

@Override
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
String number= value.toString();
int nmbr = Integer.parseInt(number);
if(nmbr < 0 || nmbr > 20)
{ 
FacesMessage msg = 
new FacesMessage("Number Length exceeds.", 
"Number should be in the range 0 to 20");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg); 
}
}
} 

input.xhtml

<!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">

<head>
<title>JSF 2 f validator</title>
</head>
<body>
<f:view>
<h:form>
<h:outputText value="Enter Number (between 0-20)"/><br/>
<h:message for="tx" style="color:red" /><br/>
<h:inputText id="tx" value="#{numBean.num}" required="true">
<f:validator validatorId="devmanuals.NumberValidatorBean" />
</h:inputText><br/><br/>
<h:commandButton value="submit" action="output" />
</h:form>
</f:view>
</body>
</html>

output.xhtml

<!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">

<head>
<title>JSF 2 f validator output</title>
</head>
<body>
<f:view>
<h:outputText value="Input Number is : #{numBean.num}" />
</f:view>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>jsfValidator</display-name>
<welcome-file-list>
<welcome-file>input.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
</web-app>

How To Run

To run this example at first you would be required to either import the WAR file (provided to you by downloading the source code below) or create a dynamic project and copied the following codes in their respective files (you may follow the above given directory structure or you may create your own) then start your installed web server and then use the following link http://localhost:8181/jsfValidator/input.jsf on your web browser or simply select your project -> Right click -> Run As -> Run On Server.

Output :

When you will execute the above example you will get the output as follows :

1. The main page will be as follows :

2. When you will input the value other than the specified range then the output will be as follows :

3. But when you will input the value within the specified range then the output will be as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics