<convertNumber> and <convertDateTime> in JSF

By: Baski Viewed: 153413 times  Printer Friendly Format    


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.



Most Viewed Articles (in JSF )

Latest Articles (in JSF)

Comment on this tutorial