Monday, September 20, 2010

Server Side Date Validator

Steps for creating ServerSide Date Validator
1) Create datevalidate.jsp file
datevalidate.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" %>
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
<!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="EnterDate.:" />
<rich:calendar id="dc_SOLD_ON" binding="#{validate.dc_SOLD_ON}" inputStyle="font-size:10pt;width:55px;height:20px"datePattern="dd-MM-yyyy" >
<f:validator validatorId="dateValidator"/>
</rich:calendar>
<h:commandButton id="cb_save" value="Save" action="#{validate.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 (DateValidate.java and DateValidator.java)
DateValidate.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package subin;

import org.richfaces.component.html.HtmlCalendar;
/**
*
* @author SubinSuresh
*/
public class DateValidate {
public void bt_SAVEActionPerformed() {
System.out.println("Debug bt_SAVEActionPerformed");
}
private HtmlCalendar dc_SOLD_ON= new HtmlCalendar();
/**
* @return the dc_SOLD_ON
*/
public HtmlCalendar getDc_SOLD_ON() {
return dc_SOLD_ON;
}
/**
* @param dc_SOLD_ON the dc_SOLD_ON to set
*/
public void setDc_SOLD_ON(HtmlCalendar dc_SOLD_ON) {
this.dc_SOLD_ON = dc_SOLD_ON;
}
}
--------------------------------------------------------------------------------------------------------------------------
DateValidator.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package subin;

import java.util.Date;
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;
import org.richfaces.component.html.HtmlCalendar;

/**
*
* @author SubinSuresh
*/
public class DateValidator implements Validator {

public String DATE_FORMAT = "dd-MM-yyyy";
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
System.out.println("Debug Inside DateValidator" + ((HtmlCalendar) component).getSubmittedValue());
String date = (String) ((HtmlCalendar) component).getSubmittedValue();
try {
Date dt = converStringToDate(date);
System.out.println("Debug Inside dt " + dt);
} catch (Exception ex) {
ex.printStackTrace();
((HtmlCalendar) component).setSubmittedValue("");
throw new ValidatorException(new FacesMessage("Invalid Date"));
}
}
/**
* Convert String to Date
* @param dateString
* @return
*/
public Date converStringToDate(String dateString) {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
java.util.Date dt = null;
try {
dt = sdf.parse(dateString);
} catch (java.text.ParseException p) {
System.out.println(p.toString());
}
return dt;
}
}
------------------------------------------------------------------------------------------------------------------
3)Mapping in faces-config.xml file
<managed-bean>
<managed-bean-name>validate</managed-bean-name>
<managed-bean-class>subin.DateValidate</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<validator>
<validator-id>dateValidator</validator-id>
<validator-class>subin.DateValidator</validator-class>
</validator>


No comments:

Post a Comment