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:
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).
NoteYou 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 theMM/dd/yyformat), and the search results page will be sent to the browser.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
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:
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.
Most Viewed Articles (in JSF )
How to open a new browser window from my JSF page?
Struts Vs JSF (A comparison of Struts against JSF)
Calling Multiple Listeners in JSF
Accessing Context Data in Beans using 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)
<convertNumber> and <convertDateTime> in JSF
Controlling Page Navigation in JSF - Static and Dynamic Navigation
Latest Articles (in JSF)
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
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
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate