Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2007 Pearson Education, Inc. All rights reserved. 1 27 Ajax-Enabled JavaServer™ Faces Web Applications.

Similar presentations


Presentation on theme: " 2007 Pearson Education, Inc. All rights reserved. 1 27 Ajax-Enabled JavaServer™ Faces Web Applications."— Presentation transcript:

1  2007 Pearson Education, Inc. All rights reserved. 1 27 Ajax-Enabled JavaServer™ Faces Web Applications

2  2007 Pearson Education, Inc. All rights reserved. 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  2007 Pearson Education, Inc. All rights reserved. 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  2007 Pearson Education, Inc. All rights reserved. 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  2007 Pearson Education, Inc. All rights reserved. 5 27.1 Introduction 27.2 Accessing Databases in Web Applications 27.2.1 Building a Web Application That Displays Data from a Database 27.2.2 Modifying the Page Bean File for the AddressBook Application 27.3 Ajax-Enabled JSF Components 27.4 AutoComplete Text Field and Virtual Forms 27.4.1 Configuring Virtual Forms 27.4.2 JSP File with Virtual Forms and a AutoComplete Text Field 27.4.3 Providing Suggestions for a AutoComplete Text Field

6  2007 Pearson Education, Inc. All rights reserved. 6 27.5 Google Maps Map Viewer Component 27.5.1 Obtaining a Google Maps API Key 27.5.2 Adding Map Viewer Component to a Page 27.5.3 JSP File with Map Viewer Component 27.5.4 Page Bean that Displays a Map in the Map Viewer Component 27.6 Wrap-Up 27.7 Web Resources

7  1992-2007 Pearson Education, Inc. All rights reserved. 7 27.1 Introduction  AddressBook Application – Three stages  Introduce Ajax-Enabled JSF components from the Java BluePrints Ajax componlibrary

8  1992-2007 Pearson Education, Inc. All rights reserved. 8 27.2 Accessing Databases in Web Applications  Many web applications access databases to store and retrieve persistent data

9  1992-2007 Pearson Education, Inc. All rights reserved. 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  1992-2007 Pearson Education, Inc. All rights reserved. 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  1992-2007 Pearson Education, Inc. All rights reserved. 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  1992-2007 Pearson Education, Inc. All rights reserved. 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  1992-2007 Pearson Education, Inc. All rights reserved. 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  1992-2007 Pearson Education, Inc. All rights reserved. 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  1992-2007 Pearson Education, Inc. All rights reserved. 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  1992-2007 Pearson Education, Inc. All rights reserved. 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  2007 Pearson Education, Inc. All rights reserved. 17 Fig. 27.1 | AddressBook application form for adding a contact.

18  2007 Pearson Education, Inc. All rights reserved. 18 Fig. 27.2 | Addresses table data.

19  2007 Pearson Education, Inc. All rights reserved. 19 Fig. 27.3 | Dialog for binding to the Addresses table.

20  2007 Pearson Education, Inc. All rights reserved. 20 Fig. 27.4 | Table component after binding it to a database table and editing its column names for display purposes.

21  2007 Pearson Education, Inc. All rights reserved. 21 Fig. 27.5 | Editing addressesRowSet ’s SQL statement.

22  2007 Pearson Education, Inc. All rights reserved. 22 Outline AddressBook.jsp (1 of 7 )

23  2007 Pearson Education, Inc. All rights reserved. 23 Outline AddressBook.jsp (2 of 7 )

24  2007 Pearson Education, Inc. All rights reserved. 24 Outline AddressBook.jsp (3 of 7 )

25  2007 Pearson Education, Inc. All rights reserved. 25 Outline AddressBook.jsp (4 of 7 )

26  2007 Pearson Education, Inc. All rights reserved. 26 Outline AddressBook.jsp (5 of 7 )

27  2007 Pearson Education, Inc. All rights reserved. 27 Outline AddressBook.jsp (6 of 7 )

28  2007 Pearson Education, Inc. All rights reserved. 28 Outline AddressBook.jsp (7 of 7 )

29  2007 Pearson Education, Inc. All rights reserved. 29 Outline SessionBean1.java (1 of 2 ) Specifies the database for the RowSet Specifies the SQL used to obtain the data that will be displayed in the Table

30  2007 Pearson Education, Inc. All rights reserved. 30 Outline SessionBean1.java (2 of 2 )

31  1992-2007 Pearson Education, Inc. All rights reserved. 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  2007 Pearson Education, Inc. All rights reserved. 32 Outline AddressBook.java (1 of 4 )

33  2007 Pearson Education, Inc. All rights reserved. 33 Outline AddressBook.java (2 of 4 ) Refreshes the addressesDataProvider so that records are sorted properly after new rows are added. Ensure that data provider is closed when application terminates

34  2007 Pearson Education, Inc. All rights reserved. 34 Outline AddressBook.java (3 of 4 ) 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

35  2007 Pearson Education, Inc. All rights reserved. 35 Outline AddressBook.java (4 of 4 )

36  1992-2007 Pearson Education, Inc. All rights reserved. 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  1992-2007 Pearson Education, Inc. All rights reserved. 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 Updates and New 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  1992-2007 Pearson Education, Inc. All rights reserved. 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  1992-2007 Pearson Education, Inc. All rights reserved. 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  2007 Pearson Education, Inc. All rights reserved. 40 Fig. 27.9 | Java BluePrints component library’s Ajax-enabled components.

41  1992-2007 Pearson Education, Inc. All rights reserved. 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  1992-2007 Pearson Education, Inc. All rights reserved. 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  1992-2007 Pearson Education, Inc. All rights reserved. 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  1992-2007 Pearson Education, Inc. All rights reserved. 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  2007 Pearson Education, Inc. All rights reserved. 45 Fig. 27.10 | Configure Virtual Forms dialog.

46  2007 Pearson Education, Inc. All rights reserved. 46 Fig. 27.11 | Virtual forms legend.

47  1992-2007 Pearson Education, Inc. All rights reserved. 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  2007 Pearson Education, Inc. All rights reserved. 48 Outline AddressBook.jsp (1 of 7 ) Configures the virtual forms on this page

49  2007 Pearson Education, Inc. All rights reserved. 49 Outline AddressBook.jsp (2 of 7 )

50  2007 Pearson Education, Inc. All rights reserved. 50 Outline AddressBook.jsp (3 of 7 )

51  2007 Pearson Education, Inc. All rights reserved. 51 Outline AddressBook.jsp (4 of 7 )

52  2007 Pearson Education, Inc. All rights reserved. 52 Outline AddressBook.jsp (5 of 7 ) Configures the AutoComplete Text Field

53  2007 Pearson Education, Inc. All rights reserved. 53 Outline AddressBook.jsp (6 of 7 )

54  2007 Pearson Education, Inc. All rights reserved. 54 Outline AddressBook.jsp (7 of 7 )

55  1992-2007 Pearson Education, Inc. All rights reserved. 55 27.4.3 Providing Suggestions for an AutoComplete Text Field  complete event handler is invoked after every keystroke in an AutoComplete Text Field – Updates the list of suggestions based on the text the user has typed so far – Receives a string containing the text the user has entered and a CompletionResult object that is used to display suggestions to the user

56  2007 Pearson Education, Inc. All rights reserved. 56 Outline AddressBook.java (1 of 3 )

57  2007 Pearson Education, Inc. All rights reserved. 57 Outline AddressBook.java (2 of 3 )

58  2007 Pearson Education, Inc. All rights reserved. 58 Outline AddressBook.java (3 of 3 )

59  2007 Pearson Education, Inc. All rights reserved. 59 Performance Tip 27.1 When using database columns to provide suggestions in an AutoComplete Text Field, sorting the columns eliminates the need to check every row in the database for potential matches. This significantly improves performance when dealing with a large database.

60  1992-2007 Pearson Education, Inc. All rights reserved. 60 27.5 Google Maps Map Viewer Component  Map Viewer Ajax-enabled JSF component – Uses the Google Maps API web service to find and display maps  Map Marker points to a location on a map

61  1992-2007 Pearson Education, Inc. All rights reserved. 61 27.5.1 Obtaining a Google Maps API Key  To use the Map Viewer component – Must have an account with Google – Register for a free account at https://www.google.com/accounts/ManageAccount https://www.google.com/accounts/ManageAccount – Must obtain a key to use the Google Maps API from www.google.com/apis/maps www.google.com/apis/maps – Key is specific to your web application; limits the number of maps the application can display per day – Must provide a URL for the application - If you are deploying the application only on Java Studio Creator 2’s built-in test server, use http://localhost:8080/http://localhost:8080/

62  1992-2007 Pearson Education, Inc. All rights reserved. 62 27.5.2 Adding a Map Viewer Component to a Page  To use the Map Viewer componen – Set its key property to the your Google Maps API key  Map Marker – From the AJAX Support Beans section of the Palette – Marks a location on a map – Must bind the marker to the map so that the marker will display on the map - Right click the Map Viewer in Design mode - Choose Property Bindings… - Select info from the Select bindable property column, then select the Map Marker from the Select binding target column. - Click Apply, then Close

63  1992-2007 Pearson Education, Inc. All rights reserved. 63 27.5.2 Adding a Map Viewer Component to a Page  Geocoding Service Object – From the AJAX Support Beans section of the Palette – Converts street addresses into latitudes and longitudes – These are used to display appropriate map

64  1992-2007 Pearson Education, Inc. All rights reserved. 64 27.5.3 JSP File with a Map Viewer Component  Map Viewer center attribute – Bound to the page bean property mapViewer_center – Manipulated in the page bean file to center the map on the desired address

65  2007 Pearson Education, Inc. All rights reserved. 65 Outline AddressBook.jsp (1 of 7 )

66  2007 Pearson Education, Inc. All rights reserved. 66 Outline AddressBook.jsp (2 of 7 )

67  2007 Pearson Education, Inc. All rights reserved. 67 Outline AddressBook.jsp (3 of 7 )

68  2007 Pearson Education, Inc. All rights reserved. 68 Outline AddressBook.jsp (4 of 7 )

69  2007 Pearson Education, Inc. All rights reserved. 69 Outline AddressBook.jsp (5 of 7 )

70  2007 Pearson Education, Inc. All rights reserved. 70 Outline AddressBook.jsp (6 of 7 ) Configures the Map Viewer component

71  2007 Pearson Education, Inc. All rights reserved. 71 Outline AddressBook.jsp (7 of 7 )

72  1992-2007 Pearson Education, Inc. All rights reserved. 72 27.5.4 Page Bean that Displays a Map in the Map Viewer Component  Geocoding Service Object – geoCode method receives an address as an argument – Returns an array of GeoPoint objects representing locations that match the address parameter – GeoPoint s provide a location’s latitude and longitude

73  2007 Pearson Education, Inc. All rights reserved. 73 Outline AddressBook.java (1 of 4 )

74  2007 Pearson Education, Inc. All rights reserved. 74 Outline AddressBook.java (2 of 4 )

75  2007 Pearson Education, Inc. All rights reserved. 75 Outline AddressBook.java (3 of 4 ) Set the query parameters to the specified first and last name Get the GeoPoint s that match the address

76  2007 Pearson Education, Inc. All rights reserved. 76 Outline AddressBook.java (4 of 4 ) Specify the map’s center location Place a marker on the map to indicate the location with the person’s name and address displayed on the marker


Download ppt " 2007 Pearson Education, Inc. All rights reserved. 1 27 Ajax-Enabled JavaServer™ Faces Web Applications."

Similar presentations


Ads by Google