Designing the Application
In this section, you create the application and add the necessary components to it.- Create a new web application project and name it ValidatorConverter. Enable the Visual Web JavaServer Faces framework.
The figure below shows the page you will create in the following steps. - From the Woodstock Basic section of the Components Palette, drag a Text Field component and drop it on the page. Set the
label
property toCelsius and set
thetext
property to0.0
.
Thelabel
andtext
properties are under the Appearance section of the Properties window. The text value is the default value displayed at runtime. - Set the Text Field component's
required
property to True by selecting the checkbox in the Properties window.
Therequired
property is under the Data section of the Properties window. A value of True requires that the user enter a value for the input field. If the user does not enter a value and tries to submit the page, a standard validation error message is returned. A red asterisk appears next to the Celsius label to indicate that the required property is set. - Right-click the Text Field component and choose Add Binding Attribute.
- Place a Label component on the page. Set the text of the label 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 water in the Fahrenheit scale. - Right-click the Static Text component and choose Add Binding Attribute.
- From the Basic section of the Palette, drag a Message component and drop it below the Fahrenheit label.
- In the Properties window for the Message component, select
textField1
as thefor
property.
The text of the Message component changes to Message summary for textField1, as shown in the following figure.
Using Validators
If your application collects information from users, for example a login and a password, then it is important that you verify the user data. The IDE provides a set of components for validating user input; you access them from the Validators section of the Components Palette. The simplest validation ensures that an input field has some sort of value.The Double Range validator tests whether the value of a numerical input is within a specified range. The data type must be floating point or convertible to floating point. Other validators include the Length Validator and the Long Range Validator.
- Use the Length Validator to validate that the length of text entered for a component is no shorter or longer than the values you specify for the validator's minimum and maximum properties. The value must be a java.lang.String.
- Use the Long Range Validator to validate that a user-entered value falls within a range of minimum and maximum values. The value must be any string value that can be converted to a Java long data type.
For this application, you want to check that the Text Field component contains at least one character before the page is submitted. You also need a message component to indicate when the validation fails. The Message component you added in the previous section will indicate to the user if the validation fails.
- Expand the Validators section of the Palette. Drag the Double Range Validator from the Palette and drop it on the Text Field component.
The double range validator is a non-visual component. The default valuedoubleRangeValidator1
appears in the Navigator window and in thevalidator
property in the Properties window, as shown in the figures below.
- In the Navigator window, select doubleRangeValidator1.
- Set the range for the validator in the Properties window:
- Set the
maximum
property to1000.0
(a very hot temperature). - Set the
minimum
property to-273.15
(i.e. Absolute Zero on the Celsius scale).
- Set the
Customizing a Standard Validator Message
You can override the built-in error messages that NetBeans IDE provides when a standard validation fails. You create a resource bundle for the project in which you want to provide a custom message. Note that this is a global override, not a component level override. This means that all required components return the same value.Customizing Messages
This section shows how to customize messages for the Length, Double Range, and Long Range validators in JSF 1.1 / J2EE 1.4. As you can see, this is a slightly longer process than adding customized messages in JSF 1.2. You use the IDE to customize the default error message for required input. First you create a resource bundle which maps keys used by the program to the strings displayed to the user, then you edit the faces-config.xml to point to the MyResources.properties resource bundle.Note: The steps in this section depend on the project being named ValidatorConverter. If you chose a different name, please adjust all uses of the project name to reflect the name of your project.
- In the Projects window, right-click ValidatorConverter and choose New > Other.
- In the New File wizard, select Other in the Categories field and Properties File in the File Types field and click Next.
- Enter MyResources in the File Name field, and type src/java/validatorconverter in the Folder field, and click Finish.
The IDE creates the resource bundle and opens the MyResources.properties file in the IDE. The MyResources.properties file provides the replacement text for the messages that are used by the components. - Close the MyResources.properties file.
- In the Files window, expand ValidatorConverter > src> java > validatorconverter, right-click MyResources.properties, and select Open to open the Key-Values Properties Editor.
In the Key-Values Properties Editor you can add key-value pairs to the resource bundle. - Click New Property.
- In the New Property dialog, type javax.faces.component.UIInput.REQUIRED in the Key field. In the Value field, type Please enter a value and then press Enter.
Important: Make sure that there are no spaces at the end of the key. Spaces prevent the application from working at run time.
For a list of all the keys for the standard messages, see Standard Message Keys below. - Click OK. The values are displayed in the Properties Editor, as shown in the figure below.
- In the Projects window, expand ValidatorConverter > Web Pages > WEB-INF. Right-click on faces-config.xml and choose Open.
- In the editing toolbar, click XML.
- In the the faces.config file, enter the following code. Note that this code contains the name of the project. If your project is not named ValidatorConverter, change the text in the code as appropriate. Note that you should change upper-case characters in your project name to lower-case characters in the message-bundle element.
<application> <message-bundle>validatorconverter.MyResources</message-bundle> </application>
The message-bundle element represents a set of localized messages. This element contains the fully-qualified path to the resource bundle containing the localized messages, in this case, validatorconverter.MyResources.
- Run the project.
Note: By default, the project has been created with the Compile on Save feature enabled, so you do not need to compile your code first in order to run the application in the IDE. For more information on the Compile on Save feature, see the Compile on Save section of the Creating, Importing, and Configuring Java Projects guide. - Delete any text in the text field and Press Enter.
Your customized message appears in the message field as shown in the figure below.