JAVA SERVER FACES (JSF)

JSF PROJECTS

JSF PROJECT 1

JSF Examples

JSF EXAMPLE

adplus-dvertising
<f:validator> Tag Example
Previous Home Next
Tag description:

A <f:validator> Tag is register a Validator instance on UIcomponent associate with the enclosing parent tag .

A Validator class implements Validator (extends EventListener) which provide a validate()

method that can perform validation (correctness checks) on a entry data and provide custom validation into the application.

validate() method should be examine the value and component

That they are passed and throw a ValidatorException containing a FacesMessage.

Code

<f:validator>

Example:

Step 1: Welcome page of Example

<%--
Name= welcomeJSF.jsp
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<%--
This file is an entry point for JavaServer Faces application.
--%>
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>r4r.co.in</title>
</head>
<body>
<h1><h:outputText value="Tag <f:validator> Example"/></h1>
<h:form id="validator">

<%-- Display error message --%>
<h:message for="validator:email" style="color:red" />

<h:panelGrid columns="2" cellspacing="2" cellpadding="2" >
<h:outputLabel value="Choose Email ID " />
<h:inputText id="email" value="#{validator.email}" required="true" >
<%-- Tag <f:validator> use here --%>
<f:validator validatorId="EmailValidator" />
</h:inputText>
<h:commandButton action="#{validator.submit()}" value="Submit" />
<h:commandButton action="#{validator.reset()}" value="Reset" />
</h:panelGrid>
<BR><BR>

<%-- Display result --%>
<h:panelGrid rendered="#{validator.flag != false}" >
Email ID: <h:outputText value="#{validator.email}" />
</h:panelGrid>
</h:form>
</body>
</html>
</f:view>

Step 2: JSF faces-config.xml file

<?xml version='1.0' encoding='UTF-8'?>
<!-- =========== JSF CONFIGURATION FILE ================================== -->
<faces-config version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<!-- == Add Validator class invoked by validatorId in JSP == -->
<validator>
<validator-id>EmailValidator</validator-id>  
<validator-class>r4r.JSF2.EmailValidator</validator-class>
</validator>
</faces-config>

Step 3: ManagedBean class for provide logic in program

/*
 * Save as a validatorBean.java
 */
package r4r.JSF2;

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

/* -- Name of Bean -- */
@ManagedBean(name = "validator")
@RequestScoped
public class validatorBean {

private String email;
private boolean flag = false;

/* --  Getter/ Setter -- */
public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public boolean isFlag() {
return flag;
}

public void setFlag(boolean flag) {
this.flag = flag;
}

/* --  Submit method -- */
public String submit() {
flag = true;
return "submit";
}

/* --  Reset Method -- */
public String reset() {
flag = false;
email = null;
return "reset";
}
}

Step 4: Validator class use for provide validation in Example

/*
 * Save as a EmailValidator.java
 * Class is used for implement Validator interface in example
 */
package r4r.JSF2;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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;
/* -- validator class must implement interface Validation, 
extends EventListener -- */
public class EmailValidator implements Validator {
@Override
public void validate(FacesContext context, 
	UIComponent component, Object value)
throws ValidatorException {

/* -- Create a Valid Email ID Pattern -- */
Pattern pattern = Pattern.compile("[_A-Za-z0-9-.]+@[a-zA-Z0-9-]+
		((\\.com)|(\\.co.in)|(\\.net)|(\\.org))");

/* --   retrieve the Email ID form the JSP page -- */
String email = (String) value;

/* -- Match the used entered email ID with our pattern -- */
Matcher matcher = pattern.matcher(email);

/* -- Create a Match and set error detail in JSP page  --  */
if (!matcher.matches()) {
FacesMessage message = new FacesMessage();
message.setDetail("Invalid Email ID entered");
//invoked a server warning in JSP
message.setSeverity(FacesMessage.SEVERITY_WARN);
throw new ValidatorException(message);
}
}
}

output:

Previous Home Next