Tuesday, August 6, 2013

Converters In Java Server Faces--How to convert form data into respective data types?

Converters in JSF :-


In a web application the data entered in a form is always of String data type.
Consider a web page form having first name,last name and Age.Here names are of String type and Age is Number data type. But when the data is sent from form to server every filed is String type.

How to convert Age field data to integer type before assigning it to managed bean/backing bean? We can explicitly convert it by using Integer.parseInt() etc.Doing this way is tedious and not reusable.

Does JSF provide any mechanism to handle this?
Yes JSF provides a mechanism to convert these String types into respective data type.

  • For implicit data types JSF provides implicit conversion
  • For non primitive types we need to convert explicitly by using Standard Converters( for converting String to Date etc.)or by creating custom converters

First we will see different standard converters.


    SNO
    NAME
    MAIL
    1
    ByteConverter
    Converts input String value to java.lang.Byte
    2
    BooleanConverter
    Converts input String value to java.lang.Boolean
    3
    BigDecimalConverter
    Converts input String value Java.lang.BigDecimal
    4
    BigIntegerConveter
    Converts input String value to java.lang.BigInteger
    5
    CharacterConverter
    Converts input String value to java.lang.Character
    6
    DateTimeConverter
    Converts input String value to java.util.Date
    7
    NumberConverter
    Converts input String value to java.lang.Number
    8
    IntegerConverter
    Converts input String value to java.lang.Integer
    9
    FloatConverter
    Converts input String value to java.lang.Float

Similarly we have EnumConverter,ShortConverter etc.

There are three different ways to use this Standard converters.
  1. Using Converter attribute in input tags
  2. Using nested sub-tag <f:converter>
  3. Using standard converters like <f:convertDateTime>,<f:convertNumber> etc.
Lets see each of the above with examples.

1.Using Converter attribute in input tags :-
  • inputText,inputSecret,inputHidden,outputText are supported tags
  • Lets takes an example of entering date and using converter attribute
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Date</title>
</head>
<body>
<f:view>
<h:form>
Date <h:inputText id="myDate" value="#{loginBean.date}"
converter="javax.faces.DateTime">
</h:inputText>
<h:commandButton value="submit" ></h:commandButton>
<h:message for="myDate"></h:message>
</h:form>
</f:view>
</body>
</html>
  • Here we have used DateTime converter attribute in input text,the value entered should be like Aug 5, 2013--> mmm d, yyyy. If we enter a value with different value it will throw error it is displayed by using <h:message> tag



2.Using nested sub-tag <f:converter> :-

<h:inputText id="myDate" value="#{loginBean.date}">
<f:converter converterId="javax.faces.DateTime" />
</h:inputText>

Here we have used converter tag inside inputText tag

3.Using standard converters :-

we will use <f:convertdateTime> tag
<h:inputText id="myDate" value="#{loginBean.date}">
<f:convertDateTime type="date" dateStyle="short" />
</h:inputText>

There are different types of dateStyles like short,full etc.
Short type will expect date as mm/dd/yyyy or mm/dd/yy .
Long type will expect date as August 5, 2013 etc, this is generally used for outputText tag.

We can change/add the behavior of this converters by developing custom converters.
This will be discussed in my next post.



Happy Learning

Please provide your valuable comments on this article and share it across your network.



No comments:

Post a Comment

Like and Share