1 / 76

Ajax-Enabled JavaServer™ Faces Web Applications

Learn how to access, configure databases, and include Ajax components in Netbeans web projects. Develop AutoComplete and Google Maps features.

hbolster
Download Presentation

Ajax-Enabled JavaServer™ Faces Web Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 27 Ajax-Enabled JavaServer™ Faces Web Applications

  2. Whatever is in any way beautiful hath its source of beauty in itself, and is complete in itself; praise forms no part of it. Marcus Aurelius Antoninus There is something in a face, An air, and a peculiar grace, Which boldest painters cannot trace. William Somerville Cato said the best way to keep good acts in memory was to refresh them with new. Francis Bacon

  3. I never forget a face, but in your case I’ll make an exception. Groucho Marx Painting is only a bridge linking the painter’s mind with that of the viewer. Eugéne Delacroix

  4. OBJECTIVES In this chapter you will learn: • To use data providers to access databases from web applications built in Netbeans. • To include Ajax-enabled JSF components in a Netbeans web application project. • To configure virtual forms that enable subsets of a form’s input components to be submitted to the server.

  5. 27.1Introduction 27.2Accessing Databases in Web Applications 27.2.1Building a Web Application That Displays Data from a Database 27.2.2Modifying the Page Bean File for the AddressBook Application 27.3Ajax-Enabled JSF Components 27.4AutoComplete TextField and Virtual Forms 27.4.1Configuring Virtual Forms 27.4.2JSP File with Virtual Forms and a AutoComplete TextField 27.4.3Providing Suggestions for a AutoComplete TextField

  6. 27.5Google Maps MapViewer Component 27.5.1Obtaining a Google Maps API Key 27.5.2Adding MapViewer Component to a Page 27.5.3JSP File with MapViewer Component 27.5.3JSP File with MapViewer Component 27.5.4Page Bean that Displays a Map in the MapViewer Component 27.6Wrap-Up 27.7Web Resources

  7. 27.1 Introduction • AddressBook Application • Three stages • Introduce Ajax-Enabled JSF components from the Java BluePrints Ajax componlibrary

  8. 27.2 Accessing Databases in Web Applications • Many web applications access databases to store and retrieve persistent data

  9. 27.2.1 Building a Web Application That Displays Data from a Database • Table component • Formats and displays data from database tables • Change the Table’s title property to specify the text displayed at the top of the Table

  10. 27.2.1 Building a Web Application That Displays Data from a Database • To add a Java DB database to a project • To create database, perform the following steps: • Select Tools > Java DB Database > Create Java DB Database…. • Enter the name of the database to create (AddressBook), a username (iw3htp4) and a password (iw3htp4), then click OK to create the database. • In the Netbeans Runtime tab (to the right of the Projects and Files tabs), the preceding steps create a new entry in the Databases node showing the URL of the database jdbc:derby://localhost:1527/AddressBook • This URL indicates that the database resides on the local machine and accepts connections on port 1527.

  11. 27.2.1 Building a Web Application That Displays Data from a Database • To add a table to a database • Use the Runtime tab to create tables and to execute SQL statements that populate the database with data • Click the Runtime tab and expand the Databases node • 2. Netbeans must be connected to the database to execute SQL statements. If Netbeans is already connected, proceed to Step 3. If Netbeans is not connected to the database, the icon appears next to the database’s URL (jdbc:derby://localhost:1527/AddressBook). In this case, right click the icon and click Connect…. • Expand the node for the AddressBook database, right click the Tables node and select Execute Command… to open a SQL Command editor in Netbeans • We provided the file AddressBook.sql in this chapter’s examples folder • Copy the SQL statements and paste them into the SQL Command editor in Netbeans • Highlight all the SQL commands, right click inside the SQL Command editor and select Run Selection • You may need to refresh the Tables node of the Runtime tab to see the new table

  12. 27.2.1 Building a Web Application That Displays Data from a Database • To configure a Table component to display database data • Right click the Table component • Select Bind to Data • Items under Selected will appear in the table • You can remove a column by selecting it and clicking <

  13. 27.2.1 Building a Web Application That Displays Data from a Database • By default, the Table’s column headings are the column names in the database table displayed in all capital letters • Can change these headings by selecting a column and editing its headerText property in the Properties window • To select a column, click the column’s name in Design mode

  14. 27.2.1 Building a Web Application That Displays Data from a Database • Checking a Table’s paginationControls property configures a Table for automatic pagination • Five rows displayed at a time • Buttons for moving forward and backward between groups of five contacts added to the bottom of the Table • Set internalVirtualForm property • Allows subsets of Form components to be submitted to server • Prevents table’s pagination controls from submitting entire form every time the user moves to the next set of contacts

  15. 27.2.1 Building a Web Application That Displays Data from a Database • Binding a Table to a data provider adds a new CachedRowSetDataProvider to the application’s node in the Outline window • CachedRowSetDataProvider • Provides a scrollable RowSet that can be bound to a Table component to display the RowSet’s data

  16. 27.2.1 Building a Web Application That Displays Data from a Database • CachedRowSet object is configured by default to execute a SQL query that selects all the data in the database table • Can edit this SQL query by expanding the SessionBean node in the Outline window and double clicking the RowSet element to open the query editor window

  17. Fig. 27.1 | AddressBook application form for adding a contact.

  18. Fig. 27.2 | Addresses table data.

  19. Fig. 27.3 | Dialog for binding to the Addresses table.

  20. Fig. 27.4 | Table component after binding it to a database table andediting its column names for display purposes.

  21. Fig. 27.5 | Editing addressesRowSet’s SQL statement.

  22. Outline AddressBook.jsp (1 of 7 )

  23. Outline AddressBook.jsp (2 of 7 )

  24. Outline AddressBook.jsp (3 of 7 )

  25. Outline AddressBook.jsp (4 of 7 )

  26. Outline AddressBook.jsp (5 of 7 )

  27. Outline AddressBook.jsp (6 of 7 )

  28. Outline AddressBook.jsp (7 of 7 )

  29. Specifies the database for the RowSet Specifies the SQL used to obtain the data that will be displayed in the Table Outline SessionBean1 .java (1 of 2 )

  30. Outline SessionBean1 .java (2 of 2 )

  31. 27.2.2 Modifying the Page Bean File for the AddressBook Application • Every row in a CachedRowSetDataProvider has its own key • Method appendRow adds a new row and returns its key • Method commitChanges of class CachedRowSetDataProvider • Applies any changes to the CachedRowSet to the database • CachedRowSetDataProvider method refresh • Re-executes the wrapped CachedRowSet’s SQL statement

  32. Outline AddressBook.java (1 of 4 )

  33. Refreshes the addressesDataProvider so that records are sorted properly after new rows are added. Ensure that data provider is closed when application terminates Outline AddressBook.java (2 of 4 )

  34. Adds new row and gets its key Uses key to specify the row to which the cursor should point Sets the value of one column (first name) Commits the changes to the database Outline AddressBook.java (3 of 4 )

  35. Outline AddressBook.java (4 of 4 )

  36. 27.3 Ajax-Enabled JSF Components • Ajax—Asynchronous JavaScript and XML • Term coined by Jesse James Garrett of Adaptive Path, Inc. • Describes a range of technologies for developing highly responsive, dynamic web applications • Separates the user interaction portion of an application from its server interaction • Enables both to proceed asynchronously in parallel • Enables Ajax web-based applications to perform at speeds approaching those of desktop applications • Makes asynchronous calls to the server to exchange small amounts of data • Allows only the necessary portions of the page to reload, saving time and resources

  37. 27.3 Ajax-Enabled JSF Components • Java BluePrints Ajax component library provides Ajax-enabled JSF components • To use the Java BluePrints Ajax-enabled components • Must download and import them • Choose Tools > Update Center • Click Next > to search for available updates • In the Available UpdatesandNew Modules area of the dialog, select BluePrints AJAX Components and click the right arrow (>) button • Click Next > and follow the prompts to accept the terms of use and download the components • When the download completes, click Next > then click Finish. • Click OK to restart the IDE.

  38. 27.3.1 Java BluePrints Component Library • To import the components into the Palette • Select Tools > Component Library Manager • Click Import… • Click Browse… • Select the ui.complib file and click Open • Click OK to import both the BluePrints AJAX Components and the BluePrints AJAX SupportBeans

  39. 27.3.1 Java BluePrints Component Library • To see the new components in the Palette, you must add the BluePrints AJAX Components • Make sure your application’s node is expanded in the Projects tab • Right click the Component Libraries node and select Add Component Library • Select the BluePrints AJAX Components library and click Add Component Library

  40. Fig. 27.9 | Java BluePrints component library’s Ajax-enabled components.

  41. 27.4 AutoComplete Text Field and Virtual Forms • AutoComplete Text Field • Provides a list of suggestions from a data source as the user types

  42. 27.4.1 Configuring Virtual Forms • Virtual forms • Used when you would like a button to submit a subset of the page’s input fields to the server • Can display multiple forms on the same page • Can specify a submitter and one or more participants for each form • When the virtual form’s submitter component is clicked, only the values of its participant components will be submitted to the server

  43. 27.4.1 Configuring Virtual Forms • To add virtual forms to a page • Right click the submitter component • Choose Configure Virtual Forms… from the pop-up menu • Click New to add a virtual form • Click in the Name column and specify the new form’s name • Double click the Submit column and change the option to Yes • this button submits the virtual form • Click OK to exit the dialog • Select all the input components that will participate in the virtual form • Right click one of the selected components and choose Configure Virtual Forms… • In the Participate column of the appropriate virtual form, change the option to Yes • Values in these components are submitted to the server when the form is submitted

  44. 27.4.1 Configuring Virtual Forms • To see the virtual forms in the Design mode • Click the Show Virtual Forms button • This displays a legend of the virtual forms on the page

  45. Fig. 27.10 | Configure Virtual Forms dialog.

  46. Fig. 27.11 | Virtual forms legend.

  47. 27.4.2 JSP File with Virtual Forms and an AutoComplete Text Field • AutoComplete Text Field • completionMethod attribute is bound to a page bean’s complete event handler • To create this handler • Right click the AutoComplete Text Field component in Design view • Select Edit Event Handler > complete

  48. Configures the virtual forms on this page Outline AddressBook.jsp (1 of 7 )

  49. Outline AddressBook.jsp (2 of 7 )

  50. Outline AddressBook.jsp (3 of 7 )

More Related