Virtual Form

Embed Size (px)

Citation preview

  • 7/28/2019 Virtual Form

    1/7

    Using Virtual Forms

    January 2006[Revision number: V2-2]Copyright 2006 Sun Microsystems, Inc.

    In this tutorial, you associate components on a login page with two virtual forms. One virtual form processes the information for creating a new login

    account and the other virtual form processes information for returning users. By associating the components that represent new users with one virtualform and the components that represent returning users with another virtual form, the application can process either group of components as if it wereon its own separate page.

    Contents

    - About Virtual Forms

    - Opening the Example Login Project

    - Associating Components With a Virtual Form- Adding a Second Virtual Form to the Page- Testing the Application

    - Doing More: Retaining and Discarding Submitted Values

    - Rules for Associating a Component with a Virtual Form

    - A Note About Virtual Forms and Page FragmentsExample used in this tutorial

    Login.zip included in the downloaded ZIPfile.

    About Virtual Forms

    Virtual forms enable you to limit the input fields that are processed when a web page is submitted. Two essential elements of a virtual form are its inputcomponents and its submission components. Input components, such as text fields, text areas, drop-down lists, and radio buttons,participate in thevirtual form. Submission components, most commonly buttons or links, submitthe virtual form. When a virtual form is submitted, the virtual form'sparticipants are processed while the other components on the page are ignored; essentially, the virtual form's participants are processed as if they wereon their own page. You can include more than one virtual form on a page, and each virtual form is processed separately.This tutorial provides step-by-step instructions for creating two virtual forms on a page. For an introduction to virtual forms, see Virtual Forms in theSun Java Studio Creator 2 IDE. For a complete sample application that uses virtual forms, including using a table component with virtual forms, seeHow to Use Virtual Forms.

    Opening the Example Login Project

    In this section, you open an example project that provides a login page. You use this project as a starting point for creating a virtual form.

    1. Open the Login.zip file and extract its contents into the Java Studio Creator project directory. On Windows computers, this directory istypically C:\Documents and Settings\\My Documents\Creator\Projects.

    2. In the Java Studio Creator IDE, choose File > Open Project, and open the Login project.

    3. When the Login project appears in the Projects window, choose Run > Run Main Project (or click the Run Main Project button in the toolbar) tobuild and run the project.

    In the browser, you should see the welcome page for the Chimney Sweep Central application.

    4. Click the Login link on the top left of the page.

    5. In the Returning Customer section (the first section), enter admin in the User Name text field and admin in the Password text field.

    6. Click the Login button.

    Figure 1 shows the results. All text fields on the page are validated, including the fields in the section for the new user account. These fieldsreturn an error because no value was entered. However, you want the application to process returning users and new users separately. Thesolution is to use virtual forms, as described in the following section.

    1

    http://developers.sun.com/prodtech/javatools/jscreator/reference/fi/2/virtual-forms.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/reference/fi/2/virtual-forms.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/virtual_forms.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/virtual_forms.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/reference/fi/2/virtual-forms.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/reference/fi/2/virtual-forms.html
  • 7/28/2019 Virtual Form

    2/7

    Figure 1 : Login Application Without Virtual Forms

    Associating Components With a Virtual Form

    By associating the components that represent the returning users with one virtual form and the components that represent new users with another virtualform, you can process the input fields in either group of components as if they were their own separate page. In this section, you create and configure avirtual form for returning users.

    1. In the Projects window of the IDE, expand Login > Web Pages and then double-click Login.jsp.

    2. In the Login page, right-click an empty space and choose Virtual Forms from the pop-up menu.

    3. Click New in the Virtual Forms dialog box.

    4. Double-click in the Color field and choose the color red from the drop-down list.

    5. Double-click in the Name field, type returningCustomer, and then press Enter.

    Figure 2 shows the Virtual Forms dialog box. Components associated with the returningCustomer virtual form will be identified with a redborder.

    2

  • 7/28/2019 Virtual Form

    3/7

    Figure 2 : Virtual Forms Dialog Box

    6. Click OK to apply the changes and close the dialog box.

    7. Click the Show Virtual Forms button ( ) in the Visual Designer toolbar.

    This action turns on the display of virtual forms. In the Visual Designer, you'll see a virtual forms legend that matches colors to particular virtualforms. The legend is especially useful if you have more than one virtual form on a page, as you will see in the following section. Now you willadd components to the virtual form.

    8. In the Visual Designer, hold down the Control key and click the User Name text field, Password text field, and the Remember My User Namecheckbox in the Returning User section.

    9. Right-click any of the three selected components and choose Configure Virtual Forms.10. Ensure that retUserNameTextField, retPasswordField, and remUserNameCheckbox appear at the top of the Configure Virtual Forms dialog box,

    as shown in Figure 3. Then double-click in the Participate field, choose Yes from the drop-down list, and click OK.

    The User Name field, Password field, and Remember My User Name checkbox now have a solid red border to indicate that they participate inthe returningCustomer virtual form. This means that these components will be processed when the virtual form is submitted. Note that onlyinput components are eligible to participate in virtual forms. Other types of input components include text areas, drop-down lists, and radiobuttons. Technically, an input component is a component that implements the EditableValueHolder interface.

    Figure 3 : Configure Virtual Forms Dialog Box

    11. In the Visual Designer, right-click the Login button and choose Configure Virtual Forms.

    12. In the Configure Virtual Forms dialog box, ensure that loginButton appears at the top. Then set the Submit field to Yes and click OK.

    The Login button has a dashed border to indicate that it submits the returningCustomer virtual form. Buttons and links are the most commonsubmission components. These components submit the web page because they implement the ActionSource interface.

    Figure 4 shows the returningCustomer virtual form's participants and submitters with color-coded borders.

    3

  • 7/28/2019 Virtual Form

    4/7

    Figure 4 : Virtual Form Color Coding

    Adding a Second Virtual Form to the Page

    Now you create a virtual form for the components in the New User section. The virtual form mechanism will process the participants of this virtualform while ignoring all other input components on the page, including the input components associated with the returningCustomer virtual form. Thistime you create the virtual form directly in the Configure Virtual Forms dialog box.

    1. Ensure that none of the components are selected in the Returning Customer section by clicking once on the page.

    2. Hold down the Control key and select the User Name, Password, and Password (Repeat) text fields in the New User section.

    3. Right-click any of the three selected components and choose Configure Virtual Forms.

    Ensure that newPasswordRepeatField, newUserNameTextField, and newPasswordField, appear at the top of the Configure Virtual Forms dialogbox.

    4. Click New.

    A new virtual form is added to the dialog box.

    5. Set the Color to green, the Name tonewUser, and Participate to Yes and then click OK.

    The name and color of the new virtual form appear in the virtual form legend. The border of the three components changes to a solid green line.

    4

  • 7/28/2019 Virtual Form

    5/7

    6. Right-click the Create New Account button and choose Configure Virtual Forms.

    7. In the entry for the newUser virtual form, set Submit to Yes and then click OK.

    The border of the button changes to a dashed green line.

    Testing the Application

    1. Run the application.

    2. Click the Login link on the top left of the page.

    3. In the Returning Customer section, enter admin in the User Name text field and admin in the Password text field, and then click the Loginbutton.

    The application logs you in and opens the Welcome page. The input fields in the New User section are not validated.

    4. Continue experimenting with the application by filling in the information for a new user account and then clicking the Create New Accountbutton.

    Doing More: Retaining and Discarding Submitted Values

    By default, whenever the user submits a virtual form that causes the same page to be redisplayed, any input fields that do not participate in that virtualform retain their submitted values. To understand how this works, try the following:

    1. Complete the three fields to create a new account.

    Before clicking the Create New Account button, however, you suspect you may have already created this user name and password for thisapplication.

    2. Without modifying the entries in the New User section, enter the same user name and password as a returning customer and click the Loginbutton.

    A message appears indicating that the user name is not recognized. The input fields in the New User section retain and display their entriesbecause they do not participate in the returningCustomer virtual form. Similarly, when you click the Create New Account button and submit the

    newUser virtual form, the input fields in the Returning Customer section retain and display their entries because they do not participate in thenewUser virtual form.NOTE: There are circumstances under which you need to override the default behavior and explicitly discard the submitted values of non-participating input fields. You do this by calling methods of the Form component in the page bean code. For details on this technique, see thetechnical article How to Use Virtual Forms.

    3. To finish signing up for the account, click the Create New Account button.

    Rules for Associating a Component with a Virtual Form

    Here are some rules you should follow when associating a component with a virtual form:

    q A virtual form can have zero or more participants.

    q A virtual form can have one or more submitters.

    q An input component can participate in multiple virtual forms.

    q A submission component can submit only one virtual form.

    q Only input components are eligible to participate in virtual forms. Likewise, in order for a component to be eligible to submit a virtual form, itmust be a submission component.

    q An input component can both participate in and submit a virtual form. For an input component to submit a web page, you must turn on thecomponent's Auto-Submit on Change feature, available on the component's pop-up menu. A component that both participates in and submits avirtual form has a solid border on the left and top sides and a dashed border on the right and bottom sides. A drop-down list is the most common

    5

    http://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/virtual_forms.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/virtual_forms.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/virtual_forms.html
  • 7/28/2019 Virtual Form

    6/7

    input component that both participates in and submits a virtual form.

    q Components associated with a virtual form can appear anywhere on a page and need not be co-located.

    A Note About Virtual Forms and Page Fragments

    In the Java Studio Creator 2 IDE, there is no support for virtual forms within a page fragment. To configure selective processing on components in apage fragment, you must set the immediate property to True on those components. This solution is sufficient to cover a number of uses cases;however, it is still limited insofar as it can only configure selective processing on one group of components.

    See Also:

    q How to Use Virtual Formsq Virtual Forms in the Sun Java Studio Creator 2 IDEq Using Page Fragments

    This page was last modified: January 25, 2006

    Sun and Third-party Trademarked Terminology

    The following Sun trademarked terms might be used in the Sun Java(tm) Studio Creator tutorials:

    q Sun Java Studio Creator integrated development environment (IDE)q Sun Java System Application Server version number(Application Server)q Java Platform, Standard Edition technology (Java SE(tm) platform)q JavaServer(tm) Faces technologyq JavaServer Pages(tm) technology (JSP(tm) technology)q Sun Java System Web Server version number(Web Server)q Java Database Connectivity software (JDBC software)q Enterprise JavaBeans(tm) specification (EJB(tm) specification)q Solaris(tm) Operating System software (Solaris OS software)

    The following third-party trademarked terms might be used in the Sun Java Studio Creator tutorials:

    q UNIX(R) softwareq SPARC(R) processor

    Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, U.S.A. All rights reserved.

    Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product that is described in this document. In particular, andwithout limitation, these intellectual property rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or moreadditional patents or pending patent applications in the U.S. and in other countries.

    U.S. Government Rights - Commercial software.

    Government users are subject to the Sun Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its supplements. Use issubject to license terms. Sun, Sun Microsystems, the Sun logo, Java and the Java Coffee Cup logo are trademarks or registered trademarks of SunMicrosystems, Inc. in the U.S. and other countries.This product is covered and controlled by U.S. Export Control laws and may be subject to the export orimport laws in other countries. Nuclear, missile, chemical biological weapons or nuclear maritime end uses or end users, whether direct or indirect, arestrictly prohibited. Export or reexport to countries subject to U.S. embargo or to entities identified on U.S. export exclusion lists, including, but not limitedto, the denied persons and specially designated nationals lists is strictly prohibited.

    Note: Sun is not responsible for the availability of third-party web sites mentioned in this document and does not endorse and is not responsible or liablefor any content, advertising, products, or other materials on or available from such sites or resources. Sun will not be responsible or liable for any damageor loss caused or alleged to be caused by or in connection with use of or reliance on any such content, goods, or services available on or through any suchsites or resources.

    Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, tats-Unis. Tous droits rservs.

    Sun Microsystems, Inc. dtient les droits de proprit intellectuels relatifs la technologie incorpore dans le produit qui est dcrit dans ce document. Enparticulier, et ce sans limitation, ces droits de proprit intellectuelle peuvent inclure un ou plus des brevets amricains lists l'adresse http://www.sun.com/patents et un ou les brevets supplmentaires ou les applications de brevet en attente aux tats-Unis et dans les autres pays. L'utilisation est soumise

    6

    http://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/virtual_forms.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/reference/fi/2/virtual-forms.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/pagefragments.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/pagefragments.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/reference/fi/2/virtual-forms.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/virtual_forms.html
  • 7/28/2019 Virtual Form

    7/7

    aux termes de la Licence. Sun, Sun Microsystems, le logo Sun, Java et le logo Java Coffee Cup sont des marques de fabrique ou des marques dposes deSun Microsystems, Inc. aux tats-Unis et dans d'autres pays.Ce produit est soumis la lgislation amricaine en matire de contrle des exportations etpeut tre soumis la rglementation en vigueur dans d'autres pays dans le domaine des exportations et importations. Les utilisations, ou utilisateurs finaux,pour des armes nuclaires,des missiles, des armes biologiques et chimiques ou du nuclaire maritime, directement ou indirectement, sont strictementinterdites. Les exportations ou rexportations vers les pays sous embargo amricain, ou vers des entits figurant sur les listes d'exclusion d'exportationamricaines, y compris, mais de manire non exhaustive, la liste de personnes qui font objet d'un ordre de ne pas participer, d'une faon directe ouindirecte, aux exportations des produits ou des services qui sont rgis par la lgislation amricaine en matire de contrle des exportations et la lis te deressortissants spcifiquement dsigns, sont rigoureusement interdites.

    Sun Microsystems n'est pas responsable de la disponibilit de tiers emplacements d'enchanement mentionns dans ce document et n'approuve pas et n'estpas responsable ou iresponsable d'aucun contenu, de la publicit, de produits, ou d'autres matriaux dessus ou fournis par de tels emplacements ouressources. Sun ne sera pas responsable ou iresponsable d'aucuns dommages ou perte causs ou allgus pour tre caus par ou en liaison avec l'utilisationde ce produit ou la confiance dans des tels contenu, marchandises, ou services disponibles sur ou par des tels emplacements ou ressources.

    7