In this tutorial, you will learn about converters and validators in JSF 1.2 (Woodstock). Converters are used to convert string input to data-type Java data types to achieve multiple purposes such as calculations. The validator is used to verify that user input falls within the specified range. You will use the Visual Web JSF framework to create an application that converts temperatures from Celsius to Fahrenheit. The application uses a converter to convert user input from a string value to a numeric Java data type that the application can use to compute. The application also uses a validator to check whether the data falls within the specified range and to report validation errors through a message component. You will then generate a WEB application that calculates the currency value and displays the formatted time value.
You will also learn how to add your own validation handlers to your application, and how to overwrite the built-in error messages provided by the IDE when standard validation fails.
This tutorial uses the following technologies and resources to operate
JavaServer Faces Components/ Java EE Platform |
1.2 and Java EE 5* 1.1 and Java EE 1.4 |
Travel Database |
Not required |
* To take advantage of the Java EE 5 features of NetBeans IDE 6.0, use an application server that is fully compatible with the Java EE 5 specification, such as the Sun Java application Server 9/glassfish.
This tutorial is written to apply to the GlassFish V2 application server. If you use a different server, consult the release notes and FAQs for known issues and solutions. To learn more about supported servers and Java EE platforms, see the release notes.
Designing Applications
In this section, you will create the application and add the necessary components to it.
Create a WEB Application project named "Validatorconverter". Enables the Visual Web JavaServer Faces framework.
Figure 1 shows the pages that you will create in the following steps.
From the basic category in the palette, drag a text field onto the page. Set the Label property to "Celsius" and the "text" property to "0.0".
The label and text properties are located under the Appearance category in the Properties window. The text value is the default display value for the runtime.
Set the Required property of the text field to True by selecting the check box in the Properties window.
The Required property is located under the data category in the Properties window. A True value requires the user to enter a value for the input field. If a user tries to submit a page without entering a value, a standard validation error message is returned. A red asterisk appears next to the Celsius label, indicating that the required value has been set.
Place a "label" component on the page. Set the label's text to "Fahrenheit".
Place a "static text" component to the right of the Fahrenheit label. Set the "text" property to "32.0" to represent the freezing point of the water in the Fahrenheit scale.
From the basic category of the palette, drag a message component under the Fahrenheit tab.
Hold down the Ctrl+shift key and drag a line from the message component to the Text field component.
The text of the message component changes to "TextField1 profile message," as shown in the following illustration:
Figure 1: Overview message
In the next section, you will add a validator to ensure that the user's input falls within the specified range.