<convertNumber> and <convertDateTime> in JSF

By: Baski Emailed: 1608 times Printed: 2076 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The JSF implementation comes with two standard converters, one for numbers and one for dates and times:

<convertNumber>: Converts strings to numbers, and vice versa. You can include optional attributes to format the numbers in various ways including as currency, as integers, and as floating-point numbers.
<convertDateTime>: Converts strings to dates or times, and vice versa. You can include optional attributes to format by using various styles and time zones.

To use one of these converters, you nest the converter tag inside the <inputText> tag. In general, you can nest a converter inside any of the input or output custom tags. The converter will be called by the JSF implementation in the Update Model Values and Render Response phases of the JSF life cycle. If the conversion succeeds, the life cycle continues. If the conversion fails, the life cycle transitions to the Render Response phase, where the originating page is rendered, and the error message displayed (if the page contains message tags).

Note You do not need to use a converter if the type of the property is a primitive type (int, double, and so on), a Boolean, a BigInteger, a BigDecimal, or a String. You do need to use a converter if the type of the property is any other object, including Date.

The JSF implementation will automatically convert input values to numbers when the bean property is some primitive numeric type. If automatic conversion will not convert the number properly, you can explicitly control conversion through the standard <convertNumber> converter tag. For example, the <convertNumber> tag has attributes that allow you to convert the input value to a currency value.

The other standard converter is the <convertDateTime> tag. By using various attributes of this tag, you can convert dates or times, in various formats, to Date or Time properties in the managed bean. Let’s modify the searchForm.jsp page to use the <convertDateTime> tag. The new <inputText> tags look like this:

<h:inputText id="departDate" value="#{flight.departDate}">
<f:convertDateTime pattern="MM/dd/yy"/>
</h:inputText>
<h:message for="departDate"/>
. . .
<h:inputText id="returnDate" value="#{flight.returnDate}">
<f:convertDateTime pattern="MM/dd/yy"/>
</h:inputText>
<h:message for="returnDate"/>

The <convertDateTime> tag is nested in the <inputText> tag. The <convertDateTime> tag has several attributes that control the date conversion. We’ve used the pattern attribute to identify the pattern of the date string that will be converted. The symbols that you can use in pattern strings are the same symbols recognized by the java.text.SimpleDateFormat class. (Can you guess what the JSF implementation uses to do the conversion?) We’ve identified that the input value will consist of the two-digit month, followed by the two-digit day, followed by the two-digit year, with forward slashes delimiting each value. Now when you click the Search button, the JSF implementation will convert the date (assuming it follows the MM/dd/yy format), and the search results page will be sent to the browser.


JSF Home | All JSF Tutorials | Latest JSF Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Baski
Compiling multiple source files and specifying classpath using javac
The equals() Method example in Java
Default Values for Data Types in Java
JSP Example to connect to MS SQL database using Tomcat Connection Pool
<convertNumber> and <convertDateTime> in JSF
Enable/Disable Scripting Elements in JSP
Using calloc() Function in C
lseek() sample program in C
Arrays of Structures example program in C
The Basic Syntax Expression Language in JSP
Initialization of Pointer Arrays in C
Functions returning non-integer values in C
Increment and Decrement Operators in C
Getting Started with C
What is JSF (JavaServer Faces)?

More Tutorials in JSF
Struts Vs JSF (A comparison of Struts against JSF)
faces-config.xml to DirectTraffic in the JSF Application
<convertNumber> and <convertDateTime> in JSF
Action listeners in JSF
Calling Multiple Listeners in JSF
Using Javascript in JSF
How to open a new browser window from my JSF page?
Servlet error : java.lang.IndexOutOfBoundsException (JSF RI 1.1_01: IndexOutOfBoundsException)
JSF - TreeNode.setID gets IllegalArgument Exception
Install and Deploy JBoss Application Server
What is JSF (JavaServer Faces)?
Differences between JavaServer Faces technology and Struts
JSF Basics
The Relationship Between JSF and Other Java EE Technologies
JSF Life Cycle

More Latest News
Most Viewed Articles (in JSF )
JSF Life Cycle
How to open a new browser window from my JSF page?
Java Bean Scopes in JSF
Install and Deploy JBoss Application Server
Action listeners in JSF
Calling Multiple Listeners in JSF
Struts Vs JSF (A comparison of Struts against JSF)
What is JSF (JavaServer Faces)?
Differences between JavaServer Faces technology and Struts
Controlling Page Navigation in JSF - Static and Dynamic Navigation
Using Javascript in JSF
faces-config.xml to DirectTraffic in the JSF Application
<convertNumber> and <convertDateTime> in JSF
JSF Basics
The Relationship Between JSF and Other Java EE Technologies
Most Emailed Articles (in JSF)
Accessing Context Data in Beans using JSF
What is JSF (JavaServer Faces)?
Servlet error : java.lang.IndexOutOfBoundsException (JSF RI 1.1_01: IndexOutOfBoundsException)
Action listeners in JSF
Install and Deploy JBoss Application Server
JSF Life Cycle
Java Bean Scopes in JSF
Calling Multiple Listeners in JSF
JSF Basics
Installing JSF
Controlling Page Navigation in JSF - Static and Dynamic Navigation
<convertNumber> and <convertDateTime> in JSF
How to open a new browser window from my JSF page?
JSF - TreeNode.setID gets IllegalArgument Exception
faces-config.xml to DirectTraffic in the JSF Application