|
|
|||||||||||||||||||||||||||||||||||||||||
Carol McDonald's Blog
«If you haven't tried NetBeans lately-- |
Main
| Sample Store Catalog Application using Visual Web Pack and the Java Persistence APIs »
Pagination of Data Sets in a Sample Application using JSF, Catalog Facade Stateless Session, and Java Persistence APIsPosted by caroljmcdonald on May 31, 2007 at 03:46 PM | Comments (5)This Sample Store Catalog app demonstrates the usage of JSF, the new Java Persistence APIs, and a Stateless Session EJB to implement pagination in a Java EE 5 application download sample code Sean Brydon Carol McDonald Pagination using Java Persistence APIsThe Java Persistence Query APIs are used to create and execute queries that can return a list of results. The query APIs also have some methods that you can use to specify the chuck of a list you desire, in particular:
Lets consider an example, assume you have an Item.java object that is a Java Persistence object entity and each item object represents a row in the database. Lets assume the database is populated with 100 items that have a productID="dog", and a client web application wants to retrieve 10 items at a time. Lets look at some code to use in this type of example.
The key line in code example 1 is
List<Item> items =
query.setParameter("pID",pID).setFirstResult(start).setMaxResults(chunkSize).getResultList();
which sets the query up and gets the results. Note the query.setFirstResult starts
counting at zero. In this example, the client, maybe a JSP page
or Servlet, would need to keep track of the index position of the list
it is showing to the users. For example if it is showing the dogs from
index position 10 through 19 on its page and the user clicked the
"next" button then the JSP would have to submit the index position that
it desired 20 and the chunk size 10 to get the dogs at index positions
20 through 29. As you can see these APIs are easy to use and very
useful.In code example 2 below, we show the Item.java object. Notice this is just a typical Java Persistence entity object, and we just show it here for completeness of the example. Now lets consider some design choices when using these APIs:
Explanation of the usage of JSF, Catalog Facade Stateless Session EJB , and Java Persistence APIs in a sample Store Catalog Application The image below shows the Catalog Listing page, which allows a user to page through a list of items in a store.
The List.jsp page uses a JSF dataTable component to display a list of catalog items The dataTable component is useful when you want to show a set of results in a table. In a JavaServer Faces application, the UIData component
(the superclass of dataTable) supports binding to a collection of
data objects. It does the
work of iterating over each record in the data source. The HTML
dataTable renderer displays the data as an HTML table. In the List.jsp web page the dataTable is defined as shown below:
The value attribute of a dataTable tag references the data to be included
in the table. The var attribute specifies a
name that is used by the components within the dataTable
tag as an alias to the data referenced in the value
attribute of dataTable. In the dataTable tag from the List.jsp
page, the value attribute points to a list
of catalog items. The var attribute points
to a single item in that list. As the UIData
component iterates through the list, each reference to dataTableItem points to the current item in the
list.The dataTable's value is bound to the items property of the item controller class, ItemController, which is defined in the faces-config.xml file:
This ItemController ManagedBean items property is defined as shown below:
The getItems() method wraps a List of items, returned from the CatalogFacade Stateless Session EJB, in a DataModel. UIData, the superclass of dataTable, supports data binding to a collection of data objects represented by a DataModel instance, which is the current value of this component itself. The data collection underlying a DataModel instance is modeled as a collection of row objects that can be accessed by a row index. The APIs provide mechanisms to position to a specified row index, and to retrieve an object that represents the data that corresponds to the current row index. The Name, Photo, and Price item properties are displayed with the column component:
The column tags represent columns of data in a UIData component. While the UIData component is iterating over the rows of data, it processes the UIColumn component associated with each column tag for each row in the table. The UIData component iterates through the list of items (item.items) and displays the names, photos, and prices. Each time UIData iterates through the list of items, it renders one cell in each column. The dataTable and column tags use facets to represent parts of the table that are not repeated or updated. These include headers, footers, and captions. The ItemController ManagedBean uses dependency injection to lookup and obtain a reference to the CatalogFacade Stateless Session EJB :
The CatalogFacade Session EJB uses the Java Persistence API EntityManager Query object to return a list of items. The Catalog Facade uses dependency injection to lookup and obtain a Container Managed EntityManager. Since the EntityManager can be container managed for EJB Session Beans, the application does not have to manage its lifecycle (i.e. call the EntityManagerFactory.create() and EntityManager.close() methods).
Since this query is used for Read-Only browsing, the transaction attribute in this example is specified as NOT_SUPPORTED. Queries using transaction-scoped entity managers outside of a transaction are typically more efficient than queries inside a transaction when the result type is an entity. The JPA Query interface provides support for pagination via the setFirstResult() and setMaxResults() methods. The ItemController ManagedBean pages through the list of items by maintaining the firstItem and batchSize attributes and passing these as parameters to the catalogFacade.getItems(firstItem, batchSize) method. The ItemController's scope is defined as session, a JSF Managedbean with session scope will be stored in the session meaning that the bean's properties will stay alive for the life of the Http Session. The ItemController itemCount property is used to get and display the number of Catologue items in the data base:
This ItemController property is defined as shown below:
The ItemController getItemCount() method calls the CatalogFacade to get the count of the list of items. The Catalog Facade Session EJB getItemCount() method uses the JPA Query interface to get the count of all items in the database item table:
A JSF commandLink is used to provide a link to click on to display the next page of items. The commandLink
tag represents an HTML hyperlink and is rendered as an HTML <a> element. The commandLink
tag is used to submit an action event to the application.
This commandLink action attribute
references an ItemController backing bean next() method that calculates
the
next page's first row number and returns a logical outcome
String, which causes the List page to display the next page's
list .
This ItemController next method is defined as shown below:
The JavaServer Faces NavigationHandler
matches the logical outcome, item_list
against the navigation rules in the application configuration resource
file to determine which page to access next. In this case, the
JavaServer Faces implementation loads the List.jsp
page after this method returns.
A JSF commandLink is used to provide a link to click on to display the previous page of items. This commandLink
action attribute
references the ItemController prev() method that calculates the
previous page's first row number and returns a logical outcome
String, which causes the List page to display the previous page of
items :
This ItemController previous() method is defined as shown below:
A JSF commandLink is used to provide a link to click on to display a page with the item details. This commandLink
action attribute
references The ItemController detailSetup() method:
The item.detailSetup () method gets the Item data from the current row of the dataModel, and returns a string which causes the Detail.jsp page to display the item details :
The JavaServer Faces NavigationHandler
matches the logical outcome, item_detail
against the navigation rules in the application configuration resource
file to determine which page to access next. In this case, the
JavaServer Faces implementation loads the Detail.jsp
page after this method returns.
The Detail.jsp uses the outputText component to display the ItemController ManagedBean's item properties:
Conclusion This concludes the sample application which demonstrates how to work with the JSF dataTable and DataModel to page through a list of Item Entities which are retrieved using the CatalogFacade Stateless Session EJB methods which use the Java Persistence API. ReferencesHere are some references to consider:
Bookmark blog post: CommentsComments are listed in date ascending order (oldest first) | Post Comment
| |||||||||||||||||||||||||||||||||||||||||
|
|