Steps for creating Server Side Address Validator
1) Create address.jsp file
address.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib prefix="jc" uri="http://jsf-components" %>
<!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>JSP Page</title>
</head>
<body>
<h:form >
<jc:AlertMessage />
<h:panelGroup>
<h:outputText id="lb_No"value="Address.:" />
<h:inputText id="tf_NO" autocomplete="off" binding="#{address.tf_NO}" >
<f:validator validatorId="addressValidator"/>
<f:validateLength minimum="10" maximum="30"/>
</h:inputText>
<h:commandButton id="cb_save" value="Save" action="#{address.bt_SAVEActionPerformed}" > </h:commandButton>
</h:panelGroup>
</h:form>
</body>
</html>
</f:view>
-------------------------------------------------------------------------------------------------------
2) Create a package subin and inside subin package create two javaclass files (Address.java and AddressValidator.java)
Address.java
/** To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package subin;
import javax.faces.component.html.HtmlInputText;
/**
*
* @author SubinSuresh
*/
public class Address {
public void bt_SAVEActionPerformed() {
System.out.println("Debug bt_SAVEActionPerformed");
}
private HtmlInputText tf_NO = new HtmlInputText();
/**
* @return the tf_NO
*/
public HtmlInputText getTf_NO() {
return tf_NO;
}
/**
* @param tf_NO the tf_NO to set
*/
public void setTf_NO(HtmlInputText tf_NO) {
this.tf_NO = tf_NO;
}
}
-------------------------------------------------------------------------------------------------------
AddressValidator.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package subin;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlInputTextarea;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
/**
*
* @author subin_s
*/
public class AddressValidator implements Validator {
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
String compValue = null;
boolean flag = false;
if (component != null && component instanceof HtmlInputText) {
compValue = (String) ((HtmlInputText) component).getSubmittedValue();
} else if (component != null && component instanceof HtmlInputTextarea) {
compValue = (String) ((HtmlInputTextarea) component).getSubmittedValue();
}
if (compValue != null && !compValue.equalsIgnoreCase("")) {
flag = compValue.matches("[a-zA-Z 0-9/.&-,]*");
}
if (!flag) {
if (component instanceof HtmlInputText) {
((HtmlInputText) component).setTitle("No special symbols are allowed here");
} else if (component instanceof HtmlInputTextarea) {
((HtmlInputTextarea) component).setTitle("No special symbols are allowed here");
} else if (component instanceof HtmlSelectOneMenu) {
((HtmlSelectOneMenu) component).setTitle("Page got some un-conditional Data");
}
throw new ValidatorException(new FacesMessage("Characters you have entered are not allowed here"));
}
}
}
-----------------------------------------------------------------------------------------------------------
3)Mapping in faces-config.xml file
<managed-bean>
<managed-bean-name>address</managed-bean-name>
<managed-bean-class>subin.Address</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<validator>
<validator-id>addressValidator</validator-id>
<validator-class>subin.AddressValidator</validator-class>
</validator>
Note: In AddressValidator.java we can allow any number character by keeping in regular expression compValue.matches("[a-zA-Z 0-9/.&-,]*")
JSF Related topics: JCaptcha in JSF, Integrating Richfaces with JSF,Getting client and server sessionId in JSF and more.....
No comments:
Post a Comment