Documente Academic
Documente Profesional
Documente Cultură
In this tutorial, you use the Sun Java Studio Creator integrated development environment (IDE) to create an application that shares data between two pages. The application prompts the user for information. The information is then stored in request bean properties and bound to the components through data binding dialog boxes and source code modification. The information is then shared and displayed using the components on the second page of the application. Contents - Using a Request Bean or a Session Bean - Creating the First Page Adding the Second Page and Page Navigation - Adding Components to the Second Page - Storing the User's Information - Retrieving the User's Information - Deploying the Application To complete this tutorial, you need to know how to create a project and design a web application. For more information, see the Getting Started With Java Studio Creator and Using Simple Page Navigation tutorials.
create later in the tutorial, and through data binding dialog boxes. The favorite sport information is set within the button1_action method. The following figure shows the page that you create in the following steps.
1. Create a new web application project and name it DataSharing. 2. From the Basic section of the Palette, drag a Label component and drop it on the page. Set its text property to Please Enter Your Name: . 3. Drag a Text Field component and drop it under the Label component. Set some of its properties in the Properties window using the following values: Property (id) label required Value firstName First Name for True
4. Place another Text Field component below the firstName text field. In the Properties window, set some of this text field's properties using the following values: Property (id) label required Value lastName Last Name for True
5. Place another Label component below the lastName text field. Set some of the label's properties using the following values: PropertyValue (id) sportLabel text Select Your Favorite Sport: 6. Place a Listbox component to the right of the sportLabel component. 7. Select listbox1DefaultOptions in the Outline window, as shown in the following figure.
Figure 2 : listbox1DefaultOptions Node in the Outline Window 8. In the Properties window, click the ellipsis () button for the options property. 9. In the listbox1DefaultOptions - options dialog box, tab to each table cell and type in the new value to modify each of the existing default item. Replace the default values using the following figure. Click OK to save the changes.
Figure 3 : listbox1DefaultOptions - options Dialog Box 10. In the Visual Designer, place a Button component below the listbox component and set its text value to Done.
the first page. The following figure shows the page you create in the following steps.
1. In the Navigation editing area, double-click the Page2.jsp icon to display the second page in the Visual Designer. 2. From the Basic section of the Palette, drag a Label component onto the page and change its text property of label to My Personal Information. Set its labelLevel property to Strong (1). 3. Place another Label component and a Static Text component beneath the first label component. Set this second Label component's text property to First Name: and leave the Static Text component with the default property values. 4. Drop another pair of Label and Static Text components below the first Static Text component. Set the Label component's text property to Last Name: and leave the Static Text component with the default property values. 5. Place a third row with a Label component and a Static Text component. Set the Label component's text property to My favorite sport is and leave the Static Text component with the default property values.
3. In the New Property Pattern dialog box, enter the following values for the three text fields: Text Field Name Type Mode Value firstName String Read/Write
4. Click OK to accept the values. 5. In the Visual Designer, right-click the firstName text field component and select Bind to Data from the pop-up menu. In the Bind to Data dialog box window, click the Bind to an Object tab and select firstName under the RequestBean1 node, as shown in the following figure.
Figure 6 : Binding to Data Dialog Box for Page1 6. Click OK. The text property is updated with the bound icon window. and the binding target value in the Properties
7. Add another new property using step 2 above and use the following values in the New Property Pattern dialog box: Text Field Name Type Mode Value lastName String Read/Write
8. Click OK to accept the values. 9. Right-click the lastName text field component and select Bind to Data from the pop-up menu. In the Bind to Data dialog box window, click the Bind to an Object tab and select the lastName property under the RequestBean1 node. Click OK. 10. Add a third new property using step 2 above and use the following values in the New Property Pattern dialog box: Text Field Name Type Mode Value favoriteSport String Read/Write
11. Double-click the Done button to edit the source code in the Java Editor. Add the following code, shown in bold, to the button1_action method. Code Sample 1: button1_action Method public String button1_action() { String favoriteSport = (String) listbox1.getSelected(); getRequestBean1().setFavoriteSport(favoriteSport); // Note: The return value below must match the name of the // connector line that was set in the Navigation Editor return "case1"; }
Note that this step illustrates an alternative way to store the data in a request bean. In this step the value for the favoriteSport component is stored by calling the favoriteSport property's setter method. The value can also be stored through the data binding dialog box, as was done in steps 5 and 9 above.
Figure 7 : Binding to Data Dialog Box for Page2 4. Click OK to accept the values. In the Properties window, note that the text property value is updated with the bound icon the binding target value. and
5. Right-click the staticText2 component next to the Last Name label component and choose Bind to Data from the pop-up menu. 6. Click the Bind to an Object tab in the Bind to Data dialog box . Select the lastName property as the binding target and click OK. 7. Click Java in the Editor toolbar to display the source code in the Java Editor. 8. In the Navigator-Page2 window at the bottom left corner of the IDE, locate the prerender() member node. You can use the Quick Search feature to locate the member by selecting the first item in the member list and start typing the first few letters of the member name in the Quick Search text field. Double-click the prerender() member node to view the method in the Java Editor. The following figure shows the method in the Navigator-Page 2 window.
Figure 8 : Navigator Window for Page 2 9. Add the following code, shown in bold. Code Sample 2: prerender Method public void prerender() { staticText3.setText(getRequestBean1().getFavoriteSport()); }
Note that this step illustrates an alternative way to retrieve the data from a request bean. In this step the value for the favoriteSport component is retrieved by calling the favoriteSport property's getter method. The value can also be retrieved through the data binding dialog box, as was done in steps 3 and 6 above.
Figure 9 : First Page of Web Application 2. Click Done to see the second page, similar to the following figure.
10
See Also:
q q q q q
Getting Started With Java Studio Creator Using Simple Page Navigation Using Dynamic Page Navigation Understanding Scope and Managed Beans Winston Prakash's Weblog - Forward vs. Redirect Dispatching in a JavaServer Faces Application
Sun Java Studio Creator integrated development environment (IDE) Sun Java System Application Server version number (Application Server) Java Platform, Standard Edition technology (Java SE(tm) platform) JavaServer(tm) Faces technology JavaServer Pages(tm) technology (JSP(tm) technology) Sun Java System Web Server version number (Web Server) Java Database Connectivity software (JDBC software) Enterprise JavaBeans(tm) specification (EJB(tm) specification) 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 q
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, and without 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 more additional 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 is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and the Java Coffee Cup logo are trademarks or registered trademarks of Sun Microsystems, 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 or import laws in other countries. Nuclear, missile, chemical biological weapons or nuclear maritime end uses or end users, whether direct or indirect, are strictly prohibited. Export or reexport to countries subject to U.S. embargo or to entities identified on U.S. export exclusion lists, including, but not limited to, the denied persons and specially designated nationals lists is strictly prohibited.
11
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 liable for any content, advertising, products, or other materials on or available from such sites or resources. Sun will not be responsible or liable for any damage or 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 such sites 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. En particulier, 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 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 de Sun Microsystems, Inc. aux tats-Unis et dans d'autres pays.Ce produit est soumis la lgislation amricaine en matire de contrle des exportations et peut 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 strictement interdites. Les exportations ou rexportations vers les pays sous embargo amricain, ou vers des entits figurant sur les listes d'exclusion d'exportation amricaines, 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 ou indirecte, aux exportations des produits ou des services qui sont rgis par la lgislation amricaine en matire de contrle des exportations et la liste de ressortissants 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'est pas responsable ou iresponsable d'aucun contenu, de la publicit, de produits, ou d'autres matriaux dessus ou fournis par de tels emplacements ou ressources. Sun ne sera pas responsable ou iresponsable d'aucuns dommages ou perte causs ou allgus pour tre caus par ou en liaison avec l'utilisation de ce produit ou la confiance dans des tels contenu, marchandises, ou services disponibles sur ou par des tels emplacements ou ressources.
12