Rss

Welcome to Ejb tutor. We have used Netbeans 6.5 to create EJB applications

Validating Data in Visual Web JSF Applications

Category :

  Designing the Application

   In this section, you create the application and add the necessary components to it.
  1. 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.
  2. From the Woodstock Basic section of the Components Palette, drag a Text Field component and drop it on the page. Set the label property to Celsius and set the text property to 0.0.
    The label and text properties are under the Appearance section of the Properties window. The text value is the default value displayed at runtime.
  3. Set the Text Field component's required property to True by selecting the checkbox in the Properties window.
    The required 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.
  4. Right-click the Text Field component and choose Add Binding Attribute.
  5. Place a Label component on the page. Set the text of the label to Fahrenheit.
  6. 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.
  7. Right-click the Static Text component and choose Add Binding Attribute.
  8. From the Basic section of the Palette, drag a Message component and drop it below the Fahrenheit label.
  9. In the Properties window for the Message component, select textField1 as the for property.
    Setting the for property for the message component The text of the Message component changes to Message summary for textField1, as shown in the following figure.
    Message Summary for textfield1
   In the next section you add a validator to ensure that the user's entry falls within a specified range.

  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.
Validators and Converters in the Palette When you use a validator (or a converter, described below in Using Converters), you may also want to use a message component to indicate when validation fails. The web application performs all the validations before it calls any value change or action listeners, such as the button action methods. If one or more validation errors occur, the value change listeners and action listeners are not called, and the web application rerenders the page so that the user can correct the problems.
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.
  1. 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 value doubleRangeValidator1 appears in the Navigator window and in the validator property in the Properties window, as shown in the figures below.
    Navigator Window Properties Window
  2. In the Navigator window, select doubleRangeValidator1.
  3. Set the range for the validator in the Properties window:

    • Set the maximum property to 1000.0 (a very hot temperature).
    • Set the minimum property to -273.15 (i.e. Absolute Zero on the Celsius scale).
In this section you enabled the application to validate a user-entered value using a Double Range Validator. In the next section you use a converter to enable the application to calculate the Fahrenheit value of a numerical string input of a Celsius temperature.

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.
  1. In the Projects window, right-click ValidatorConverter and choose New > Other.
  2. In the New File wizard, select Other in the Categories field and Properties File in the File Types field and click Next.
  3. 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.
  4. Close the MyResources.properties file.
  5. 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.
  6. Click New Property.
  7. 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.
  8. Click OK. The values are displayed in the Properties Editor, as shown in the figure below.
    Properties Editor for MyResources.properties
  9. In the Projects window, expand ValidatorConverter > Web Pages > WEB-INF. Right-click on faces-config.xml and choose Open.
  10. In the editing toolbar, click XML.
  11. 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.
  12. 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.
  13. Delete any text in the text field and Press Enter.
    Your customized message appears in the message field as shown in the figure below.
    Custom Validator Message

Standard Message Keys

In the NetBeans IDE, you can use a resource bundle to handle informational messages that occur as a result of conversion, validation, or other application actions during the request processing lifecycle. The keys for the standard messages are listed in the following table.
Key Description
javax.faces.component.UIInput.CONVERSION Conversion error occurred
javax.faces.component.UIInput.REQUIRED Value is required
javax.faces.component.UISelectOne.INVALID Value is not a valid option
javax.faces.component.UISelectMany.INVALID Value is not a valid option
javax.faces.validator.NOT_IN_RANGE Specified attribute is not between the expected values of {0}and {1}
javax.faces.validator.DoubleRangeValidator.MAXIMUM Value is greater than allowable maximum of {0}
javax.faces.validator.DoubleRangeValidator.MINIMUM Value is less than allowable minimum of {0}
javax.faces.validator.DoubleRangeValidator.TYPE Value is not of the correct type
javax.faces.validator.LengthValidator.MAXIMUM Value is greater than allowable maximum of {0}
javax.faces.validator.LengthValidator.MINIMUM Value is less than allowable minimum of {0}
javax.faces.validator.LongRangeValidator.MAXIMUM Value is greater than allowable maximum of {0}
javax.faces.validator.LongRangeValidator.MINIMUM Value is less than allowable minimum of {0}
javax.faces.validator.LongRangeValidator.TYPE Value is not of the correct type

JSF Popular Components

Category :

  Table 1: Input Components
Component Description Palette Section
Text Field An input field for a single line of text. (Woodstock) Basic
Text Area An input field for multiple lines of text. (Woodstock) Basic
Drop Down List A drop-down menu, also referred to as a combo box. (Woodstock) Basic
Listbox A list from which the user can select either one item or multiple items, depending on how the component is configured. (Woodstock) Basic
Checkbox A single-character box that the user can either select (check) or clear. (Woodstock) Basic
Radio Button A single radio button that the user can select (check). (Woodstock) Basic
Password Field An input field that echoes the input characters with a replacement character to mask the input. (Woodstock) Basic
File Upload A component with a text input field and a Browse button that displays a file chooser for specifying a file to upload. The application uploads the specified file when the user submits the page. (Woodstock) Basic
Add Remove Two lists (one for available options, one for selected options) with buttons to move the options between the lists, and to order the selected options. (Woodstock) Composite
Calendar An input field and calendar for choosing dates. (Woodstock) Basic
  Table 2: Display Components
Component Description Palette Section
Label Text field that can be associated with an input field and for which you can specify a weak, medium, or strong font style. (Woodstock) Basic
Static Text Field for displaying text. (Woodstock) Basic
Image Inline image. (Woodstock) Basic
Message Text field that is linked to a specific component for displaying validation errors and other messages about that component. (Woodstock) Basic
Message Group Text field for displaying runtime error messages, program generated error messages, and, optionally, validation errors and other messages about components that are on the page. (Woodstock) Basic
Page Alert Similar to an Alert component (below), but intended to display the icon and information on a separate page. (Woodstock) Layout
Alert Displays an icon and informational text such as a warning, an error, or the successful completion of some event. (Woodstock) Composite
Hidden Field An invisible field that can be used to hold data or pass information to the server. (Woodstock) Basic
Page Separator A horizontal line that resizes to any page width selected by the user (Woodstock) Layout
Form Used to add a form (Woodstock) Layout
Inline Help Provides short help information that appears inline on your pages. (Woodstock) Composite
Bubble Help Designed to appear during a mouse over. You can manually set a delay or how long the bubble should remain visible. (Woodstock) Composite
Accordion Used for vertical tabbing. (Woodstock) Composite
  Table 3: Grouping Components
Component Description Palette Section
Checkbox Group Displays two or more checkboxes in a grid layout. (Woodstock) Basic
Radio Button Group Displays two or more radio buttons in a grid layout and ensures that only one button can be selected at a time. (Woodstock) Basic
Table, Table Row Group, and Table Column Displays data from a composite data type such as a database table or an array. (Woodstock) Basic
Grid Panel Organizes the components within a layout of rows and columns. (Woodstock) Layout
Group Panel Groups a set of components in flow layout mode. (Woodstock) Layout
Layout Panel Use to group a set of components in flow layout mode or grid layout mode. (Woodstock) Layout
Tab Set and Tab Displays different layouts on the same page. Also can be used as a navigational tool. (Woodstock) Layout
Page Fragment Box Groups components that you want to consistently display on two or more pages. (Woodstock) Layout
Property Sheet, Property Sheet Section, and Property Lays out a single column of labeled components quickly, and divides the components into sections. (Woodstock) Layout
Breadcrumbs Lays out a series of link components separated by right angle brackets (>). (Woodstock) Composite
  Table 4: Action Components
Component Description Palette Section
Button Button that submits the associated form. (Woodstock) Basic
Hyperlink Text field that submits a URL or submits a form. (Woodstock) Basic
Image Hyperlink Image that submits a URL or submits a form. (Woodstock) Basic
Tab Subcomponent of a Tab Set or a Tab. A tab can optionally submit a URL or submit a form. (Woodstock) Layout
Common Task A link to a Visual web JSF page where a user can perform a task. (Woodstock) Composite

Creating a Web application using visual web JSF

Category :

Creating a Sample Enterprise Application

Category :

  1. Go to the File -> New Project