<convertNumber> and <convertDateTime> in JSF
By: Baski in JSF Tutorials on 2007-10-06
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.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Struts Vs JSF (A comparison of Struts against JSF)
Calling Multiple Listeners in JSF
<convertNumber> and <convertDateTime> in JSF
faces-config.xml to DirectTraffic in the JSF Application
JSF - TreeNode.setID gets IllegalArgument Exception
Servlet error : java.lang.IndexOutOfBoundsException (JSF RI 1.1_01: IndexOutOfBoundsException)
How to open a new browser window from my JSF page?
Accessing Context Data in Beans using JSF
Install and Deploy JBoss Application Server
Controlling Page Navigation in JSF - Static and Dynamic Navigation
Comments