data sharing twopages

Upload: nyellutla

Post on 14-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Data Sharing Twopages

    1/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) tocreate an application that shares data between two pages. The application prompts the user forinformation. The information is then stored in request bean properties and bound to the componentsthrough data binding dialog boxes and source code modification. The information is then shared anddisplayed 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 PageNavigation

    - Adding Components to the Second Page

    - Storing the User's Information

    - Retrieving the User's Information

    - Deploying the ApplicationTo complete this tutorial, you need to know how to create a project and design a web application. Formore information, see the Getting Started With Java Studio Creator and Using Simple Page Navigationtutorials.

    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 shortlived, you can use the request bean to store the data. Note, however, that the request bean lasts only forthe life of one page request. Use of a request bean is advantageous when there is a large amount of datainvolved. 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 thatdata. The data persists until the browser session is ended. Use a session bean to store the data if you areredirected, instead of forwarded, to another page that needs that data. Note that forward dispatch is thedefault 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 buttonaction is used to navigate to the second page. For additional information about managed beans, see theUnderstanding 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 firstname, last name, and favorite sport. The information is stored through request bean properties, which you

    1

    http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/jscintro.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/simplepagenav.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/scopes.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/scopes.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/simplepagenav.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/jscintro.html
  • 7/30/2019 Data Sharing Twopages

    2/12

    create later in the tutorial, and through data binding dialog boxes. The favorite sport information is setwithin the button1_action method. The following figure shows the page that you create in thefollowing 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 Value

    (id) firstName

    labelFirst

    Name

    required 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 Value

    (id) lastName

    labelLast

    Name

    required for True

    2

  • 7/30/2019 Data Sharing Twopages

    3/12

    5. Place another Label component below the lastName text field. Set some of the label's properties

    using the following values:

    PropertyValue

    (id) sportLabeltext 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 theOutline 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 newvalue to modify each of the existing default item. Replace the default values using the followingfigure. 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 textvalue to Done.

    3

  • 7/30/2019 Data Sharing Twopages

    4/12

    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-upmenu.

    The Navigation Editor displays the Page1.jsp icon, which represents the page you created in theprevious section.

    2. Right-click an empty space in the Navigation editing area and choose New Page from the pop-upmenu.

    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 connectorname ofcase1.

    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

    4

  • 7/30/2019 Data Sharing Twopages

    5/12

    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 theVisual Designer.

    2. From the Basic section of the Palette, drag a Label component onto the page and change its textproperty 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 Textcomponent with the default property values.

    4. Drop another pair of Label and Static Text components below the first Static Text component. Setthe Label component's text property to Last Name: and leave the Static Text component withthe default property values.

    5. Place a third row with a Label component and a Static Text component. Set the Labelcomponent's text property to My favorite sport is and leave the Static Text componentwith 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 therequest 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 chooseAdd > Property.

    5

  • 7/30/2019 Data Sharing Twopages

    6/12

    3. In the New Property Pattern dialog box, enter the following values for the three text fields:

    Text Field Value

    Name firstNameType StringMode 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 Datafrom the pop-up menu. In the Bind to Data dialog box window, click the Bind to an Object taband 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 and the binding target value in the Propertieswindow.

    7. Add another new property using step 2 above and use the following values in the New PropertyPattern dialog box:

    Text Field Value

    Name lastNameType StringMode Read/Write

    6

  • 7/30/2019 Data Sharing Twopages

    7/12

    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 lastNameproperty under the RequestBean1 node. Click OK.

    10. Add a third new property using step 2 above and use the following values in the New PropertyPattern dialog box:

    Text Field Value

    Name favoriteSportType StringMode 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 settermethod. The value can also be stored through the data binding dialog box, as was done in steps 5and 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 fromthe 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.

    7

  • 7/30/2019 Data Sharing Twopages

    8/12

    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 andthe binding target value.

    5. Right-click the staticText2 component next to the Last Name label component and choose Bind toData from the pop-up menu.

    6. Click the Bind to an Object tab in the Bind to Data dialog box . Select the lastName property asthe 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 firstitem in the member list and start typing the first few letters of the member name in the QuickSearch text field. Double-click the prerender() member node to view the method in the JavaEditor. The following figure shows the method in the Navigator-Page 2 window.

    8

  • 7/30/2019 Data Sharing Twopages

    9/12

    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 thisstep the value for the favoriteSport component is retrieved by calling the favoriteSport property'sgetter method. The value can also be retrieved through the data binding dialog box, as was done insteps 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.

    9

  • 7/30/2019 Data Sharing Twopages

    10/12

    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

  • 7/30/2019 Data Sharing Twopages

    11/12

    See Also:

    q Getting Started With Java Studio Creatorq Using Simple Page Navigationq Using Dynamic Page Navigationq Understanding Scope and Managed Beansq 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 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. Allrights reserved.

    Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product that isdescribed in this document. In particular, and without limitation, these intellectual property rights mayinclude one or more of the U.S. patents listed at http://www.sun.com/patents and one or more additionalpatents 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 applicableprovisions of the FAR and its supplements. Use is subject to license terms. Sun, Sun Microsystems, the Sunlogo, 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 maybe subject to the export or import laws in other countries. Nuclear, missile, chemical biological weapons ornuclear maritime end uses or end users, whether direct or indirect, are strictly prohibited. Export or reexportto countries subject to U.S. embargo or to entities identified on U.S. export exclusion lists, including, but notlimited to, the denied persons and specially designated nationals lists is strictly prohibited.

    11

    http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/jscintro.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/simplepagenav.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/dynpagenav.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/scopes.htmlhttp://blogs.sun.com/roller/page/winston?entry=relative_url_problems_forward_vshttp://blogs.sun.com/roller/page/winston?entry=relative_url_problems_forward_vshttp://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/scopes.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/dynpagenav.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/simplepagenav.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/jscintro.html
  • 7/30/2019 Data Sharing Twopages

    12/12

    Note: Sun is not responsible for the availability of third-party web sites mentioned in this document and doesnot endorse and is not responsible or liable for any content, advertising, products, or other materials on oravailable 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 servicesavailable 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 leproduit qui est dcrit dans ce document. En particulier, et ce sans limitation, ces droits de propritintellectuelle peuvent inclure un ou plus des brevets amricains lists l'adresse http://www.sun.com/patentset un ou les brevets supplmentaires ou les applications de brevet en attente aux tats-Unis et dans les autrespays. L'utilisation est soumise aux termes de la Licence. Sun, Sun Microsystems, le logo Sun, Java et le logoJava 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 desexportations et peut tre soumis la rglementation en vigueur dans d'autres pays dans le domaine desexportations 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 entitsfigurant sur les listes d'exclusion d'exportation amricaines, y compris, mais de manire non exhaustive, laliste de personnes qui font objet d'un ordre de ne pas participer, d'une faon directe ou indirecte, auxexportations des produits ou des services qui sont rgis par la lgislation amricaine en matire de contrledes exportations et la liste de ressortissants spcifiquement dsigns, sont rigoureusement interdites.

    Sun Microsystems n'est pas responsable de la disponibilit de tiers emplacements d'enchanementmentionns dans ce document et n'approuve pas et n'est pas responsable ou iresponsable d'aucun contenu, dela 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 causpar ou en liaison avec l'utilisation de ce produit ou la confiance dans des tels contenu, marchandises, ouservices disponibles sur ou par des tels emplacements ou ressources.

    12