Sunteți pe pagina 1din 12

Sharing Data Between Two Pages

January 2006 [Revision number: V2-3]


Copyright 2006 Sun Microsystems, Inc.

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.

Using a Request Bean or a Session Bean


You can use either a request bean or a session bean to send data between two pages. If the data is short lived, you can use the request bean to store the data. Note, however, that the request bean lasts only for the life of one page request. Use of a request bean is advantageous when there is a large amount of data involved. The memory used to store the data will be released once the page request is completed. If the data needs to exist longer than one page request, the session bean is a better option for storing that data. The data persists until the browser session is ended. Use a session bean to store the data if you are redirected, instead of forwarded, to another page that needs that data. Note that forward dispatch is the default navigation mechanism in a JavaServer Faces web application. For this tutorial, the request bean is used since the data is shared between two pages only and a button action is used to navigate to the second page. For additional information about managed beans, see the Understanding Scope and Managed Beans tutorial.

Creating the First Page


In this section you create a new project and its first page. The page you create prompts for the user's first name, last name, and favorite sport. The information is stored through request bean properties, which you

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.

Figure 1 : First Page

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.

Adding the Second Page and Page Navigation


In this section you add another page to the application and specify the navigation between the first and second pages. The second page is where the user's information is displayed. 1. Right-click an empty space in the Visual Designer area and choose Page Navigation in the pop-up menu. The Navigation Editor displays the Page1.jsp icon, which represents the page you created in the previous section. 2. Right-click an empty space in the Navigation editing area and choose New Page from the pop-up menu. 3. Click OK in the Select Page Name dialog box to accept the default name, which is Page2. 4. Click the Page1.jsp icon to enlarge it. 5. Click button1 and drag a line to the Page2.jsp icon. Press Enter to accept the default connector name of case1. The following figure shows the button linked to the connector.

Figure 4 : Creating Page Navigation Between the Two Pages

Adding Components to the Second Page


Now add the components for the second page, which displays the information that the user provided on

the first page. The following figure shows the page you create in the following steps.

Figure 5 : Second Page

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.

Storing the User's Information


The first name and last name values that the user enters need to be stored in property objects in the request bean so that they can be shared with the second page of the application. 1. Click the Page1 tab to display the first page in the Visual Designer. 2. In the Projects window, right-click the RequestBean node under the DataSharing node and choose Add > Property.

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.

Retrieving the User's Information


Now bind the static text components on the second page so that the data that the user enters is displayed. 1. Click the Page2 tab to view the second page in the Visual Designer. 2. Right-click the staticText1 component next to the First Name label and choose Bind to Data from the pop-up menu. 3. In the Bind to Data dialog box, click the Bind to an Object tab. Under the RequestBean1 node, select the firstName property as the binding target, as shown in the following figure.

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.

Deploying the Application


Now, deploy and test the application. 1. On the first page, type your first name, last name, and choose your favorite sport from the list box, as illustrated in the following figure.

Figure 9 : First Page of Web Application 2. Click Done to see the second page, similar to the following figure.

Figure 10 : Second Page of Web Application

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

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 q q q q q q q q

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

UNIX(R) software 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, 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

S-ar putea să vă placă și