Introduction
f:convertDateTime is used for validating the date input.
also read:
If the user enters any invlid input, it will throw the error message to the screen. Also it allows the user to specify the apttern of date format using the ‘pattern’ attribute.
JSP File (index.jsp)
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%> <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%> <html> <body> <f:view> <h:form> <h:message for="dateTime"/> <h:inputText id="dateTime" value="#{jsfBean.number}"> <f:convertDateTime type="date" pattern="dd/mm/yyyy"/> </h:inputText> <h:commandButton action="#{jsfBean.submit}" value="submit"/> </h:form> </f:view> </body> </html>
JavaBean (JavaBeatJsfBean.java)
package javabeat.jsf; /** * source : www.javabeat.net */ public class JavaBeatJsfBean { private String field; public String getField() { return field; } public void setField(String field) { this.field = field; } public String submit() { System.out.println(this.field); return "success"; } }
faces-config.xml
<?xml version='1.0' encoding='UTF-8'?> <faces-config version="1.2" 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_1_2.xsd"> <managed-bean> <managed-bean-name>jsfBean</managed-bean-name> <managed-bean-class>javabeat.jsf.JavaBeatJsfBean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean> </faces-config>
view in the browser