Using Implicit Automatic Form Validation in JSF 2.0

In implicit autometic approach we need to define bean properties to be simple standard types such as – int, long, double, boolean etc. The system attempts to convert automatically and if there is any conversion error, form will redisplayed and error message stored automatically.

We need to use requiredMessage attribute to display customize error message if we want a field value must not be empty and converterMessage attribute to display customize error message if there is any type conversion error.
The h:message returns empty string if there is no message.

Bean File
ProductBean2.java

package model;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class ProductBean2 {

    private String pid, pname;
    private double price;
    private int qty;

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getQty() {
        return qty;
    }

    public void setQty(int qty) {
        this.qty = qty;
    }

//Action Controller
    public String doPurchase() {

        //apply business logic
        return ("show-purchase2");//redirect to show-purchase2.xhtml

    }
}

productentry2.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Product Entry</title>
        <link href="./css/default.css" 
              rel="stylesheet" type="text/css"/> 
    </h:head>
    <h:body>

        <f:view>
            <h:form>
                <h:panelGrid columns="3">
                    <h:outputLabel>Product ID:</h:outputLabel>
                    <h:inputText value="#{productBean2.pid}" required="true"
                                 requiredMessage="You must enter a Product ID"
                                 id="productID"/>
                    <h:message for="productID" styleClass="error"/>
                    <h:outputLabel>Product Name:</h:outputLabel>
                    <h:inputText value="#{productBean2.pname}" required="true"
                                 requiredMessage="You must enter a Product Name"
                                 id="productName"/>
                    <h:message for="productName" styleClass="error"/>
                    <h:outputLabel>Purchase Price:</h:outputLabel>
                    <h:inputText value="#{productBean2.price}" required="true"
                                 requiredMessage="You must enter an Purchase Price"
                                 converterMessage="Purchase Price must be a number"
                                 id="price"/>
                    <h:message for="price" styleClass="error"/>
                    <h:outputLabel>Quantity:</h:outputLabel>
                    <h:inputText value="#{productBean2.qty}" required="true"
                                 requiredMessage="You must enter a Quantity"
                                 converterMessage="Quantity must be a whole number"
                                 id="qty"/>
                    <h:message for="qty" styleClass="error"/>

                </h:panelGrid>
                <h:commandButton value="Purchase"
                                 action="#{productBean2.doPurchase}"/>
            </h:form>
        </f:view>
    </h:body>
</html>

show-purchase2.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Display Purchase</title>
    </h:head>
    <h:body>
        <f:view>
            <h:form>
                <h:outputLabel value="Purchase Info"/>
                <h:panelGrid columns="2">
                    <h:outputLabel>Product ID:</h:outputLabel>
                    <h:outputLabel value="#{productBean2.pid}"/>
                    <h:outputLabel>Product Name:</h:outputLabel>
                    <h:outputLabel value="#{productBean2.pname}"/>
                    <h:outputLabel>Purchase Price:</h:outputLabel>
                    <h:outputLabel value="#{productBean2.price}"/>
                    <h:outputLabel>Quantity:</h:outputLabel>
                    <h:outputLabel value="#{productBean2.qty}"/>                   
                </h:panelGrid>
            </h:form>
        </f:view>
    </h:body>
</html>

Screenshot:

jsf_implicit_form_validation

 

admin

Hello! I am Md. Abdul Bari; founder and admin of javaknowledge; currently providing training as a J2EE faculty in The Computers Ltd. Dhaka, Bangladesh under an IT Scholarship project provided by IDB-BISEW. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

More Posts - Website

Follow Me:
TwitterFacebookGoogle Plus

5,104` Total Views

Leave a Reply

Your email address will not be published. Required fields are marked *


eight × 9 =