June 2007 Archives
Sample Application using JSF, Spring 2.0, and Java Persistence APIs
Posted by caroljmcdonald on June 19, 2007 at 12:53 PM | Permalink
| Comments (30)
Pagination of Data Sets in a Sample Application using JSF, Spring
2.0, and Java Persistence APIs on Glassfish
This Sample Store Catalog app demonstrates the usage of JavaServer
Faces, the new Java Persistence APIs, and Spring 2.0 to implement
pagination. I took this example Pagination
of Data Sets in a Sample Application using JSF, Catalog Facade
Stateless Session, and Java Persistence APIs and modified it
slightly to use a Spring Bean instead of a EJB 3.0 Stateless Session
Bean. If you compare the two, you will see that the code is
almost the same, the main difference is the extra xml configuration for
Spring, and for me it wasn't easy to get the .xml
configuration right. It took looking at several articles, blogs,
examples, and some trial and error to get it working. (see the
references at the end).
download the SpringJPA sample application code
Note: I updated the Spring JSF integration part of this
application to use the
org.springframework.web.jsf.DelegatingVariableResolver instead
of a JSF Managed Bean ServiceLocator in order to get a
reference to the CatalogService Spring
Bean from the Spring
application context.
Explanation of the usage of JSF, Spring 2.0 , 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:
(Note: Red
colors
are for Java EE
tags, annotations code, Blue for Spring
specific
and Green
for my code
or variables)
Code Sample from: List.jsp
|
<h:dataTable value='#{item.items}' var='dataTableItem'
border="1"
cellpadding="2" cellspacing="0">
|
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:
| Code Sample from: faces-context.xml |
<managed-bean>
<managed-bean-name>item</managed-bean-name>
<managed-bean-class>
sessionpagination.ItemController
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>catalogService</property-name>
<value>#{catalogService}</value>
</managed-property>
</managed-bean>
|
This ItemController ManagedBean items
property is defined as shown below:
Code Sample from: ItemController.java |
public DataModel
getItems()
{
if (model==null ||
index != firstItem){
model=getNextItems();
}
return this.model;
}
public DataModel getNextItems()
{
model = new ListDataModel(catalogService.getItems(firstItem,batchSize));
index =
firstItem;
return this.model;
}
|
The getItems()
method wraps a List of item objects, returned from the catalogService,
in a DataModel.
UIData,
the superclass of dataTable,
supports data binding to a
collection of data objects represented by a DataModel
instance. 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 Item
properties Name, Photo, and price are
displayed with the column
component:
Code Sample from: List.jsp
|
<h:column>
<f:facet name="header">
<h:outputText value="Price"/>
</f:facet>
<h:outputText value="#{dataTableItem.price}"/>
</h:column>
|
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 dataTableItem.price.
Each
time UIData iterates through the list of items, it renders one cell in
each column.
The dataTable and column tags use facet
to represent parts of the
table that are not repeated or updated. These include headers,
footers,
and captions.
The recommended way to integrate Spring with JSF is to
configure the Spring DelegatingVariableResolver
in the
faces-context.xml. The <application>
<variable-resolver>
elements in a faces-config.xml file allows a Faces-based application to
register a custom replacement class for the implementation of the
standard Faces VariableResolver
implementation. The Spring DelegatingVariableResolver
first delegates to the original resolver of the underlying JSF
implementation, then to the Spring root
WebApplicationContext. This
allows you to configure Spring Beans as managed properties of your JSF
Managed Beans. For example, below the catalogService
Spring
Bean is configured as a managed property of the
ItemController
JSF ManagedBean:
| Code Sample from: faces-context.xml |
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>
<managed-bean>
<managed-bean-name>item</managed-bean-name>
<managed-bean-class>
sessionpagination.ItemController
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>catalogService</property-name>
<value>#{catalogService}</value>
</managed-property>
</managed-bean>
|
The catalogService,
and its implementation CatalogDAO,
is defined as a Spring
bean in the Spring configuration resource file /WEB-INF/applicationContext.xml :
| Code Sample from: applicationContext.xml
|
<bean id="catalogService" class="service.CatalogDAO"/>
<bean name="itemController" class="sessionpagination.ItemController">
<property
name="catalogService">
<ref bean="catalogService"/>
</property>
</bean>
</beans>
|
<property
name="catalogService"> refers to the ItemController setCatalogService
method. The Spring root
WebApplicationContext will inject the catalogService
Spring Bean
into the catalogService
property
of the ItemController
JSF
ManagedBean :
Code Sample from: ItemController.java |
public class ItemController
{
private CatalogService catalogService ;
public void setCatalogService(CatalogService
catalogService) {
this.catalogService =
catalogService;
}
|
For more information on using JSF with Spring, see Spring
- Java/J2EE Application Framework Integrating with JavaServer Faces
.
Using the Java Persistence API (JPA) with Spring 2.0
The Spring bean CatalogDAO
uses the Java
Persistence API
EntityManager
Query object to return a list of items.
The CatalogDAO annotates the field private EntityManager em;
with @PersistenceContext
, which causes an entity manager to be
injected. (note that using the
@PersistenceContext annotation is the same
way an
Entity Manager is injected for a EJB
3.0 Session Bean.)
| Code Sample from: CatalogDAO.java |
@Repository
@Transactional
public class CatalogDAO
implements CatalogService
{
@PersistenceContext(unitName="PetCatalogPu")
private EntityManager
em;
public List<Item> getItems(int
firstItem,int batchSize) {
Query q = em.createQuery("select
object(o)
from Item as o");
q.setMaxResults(batchSize);
q.setFirstResult(firstItem);
List<Item> items=
q.getResultList();
return
items;
}
|
The Java Persistence Query
APIs are used to create and execute queries that can return a
list of results. The JPA Query interface provides
support for pagination via the setFirstResult() and setMaxResults()
methods: q.setMaxResults(int maxResult)
sets the maximum number of results to retrieve. q.setFirstResult(int startPosition)
sets the position of the first result to retrieve.
In the code below, we show the Item
entity class which maps to the ITEM table that stores the
item instances. This is a
typical Java Persistence entity object. There are two requirements for
an entity:
- annotating the class with an @Entity
annotation.
- annotating the primary key identifier with @Id
Because the fields name, description.... are basic mappings from the
object fields to columns of the same name in the database table, they
don't have to be annotated. The O/R relationships with Address
and Product
are also annotated. For more information on
defining JPA entities see Pro
EJB 3: Java Persistence API book.
| Code Sample from: Item.java |
@Entity
public class Item
implements java.io.Serializable {
@Id
private String itemID;
private String name;
private String description;
private String imageurl;
private String imagethumburl;
private BigDecimal price;
@OneToOne(cascade={CascadeType.PERSIST})
private Address
address;
@ManyToOne
private Product
product;
public Item() { }
public String getItemID() {
return itemID;
}
public void setItemID(String itemID) {
this.itemID = itemID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String
description) {
this.description = description;
}
public BigDecimal getPrice() {
return
price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public void setAddress(Address address) {
this.address = address;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
...
}
|
The ItemController
ManagedBean pages through the list of Items
by
maintaining the firstItem and batchSize attributes and passing these as
parameters to the CatalogService getItems(firstItem,
batchSize) method.
The ItemController's
<managed-bean-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 Catolog items
in the data base on the List.jsp page:
| Code Sample from: List.jsp |
<h:outputText
value="Item
#{item.firstItem +
1}..#{item.lastItem} of
#{item.itemCount}"/>
|
This ItemController
ItemCount property
is defined as shown below:
| Code Sample from: ItemController.java |
public int getItemCount()
{
int count = catalogService.getItemsCount();
return
count;
}
|
The ItemController
getItemCount()
method calls the CatalogService
interface to get
the count of the list of items. The CatalogDAO
Spring bean getItemCount()
method uses the JPA Query interface to get the count of
all items in the database item table:
Code Sample from: CatalogDAO.java |
public class CatalogDAO
implements CatalogService
{
. . .
public int getItemCount()
{
Query q =
entityManager.createQuery("select
count(o) from Item as
o");
int count =
((Long)q.getSingleResult()).intValue();
return count;
}
|
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.
| Code Sample from: List.jsp |
<h:commandLink
action="#{item.next}"
value="Next
#{item.batchSize}"
rendered="#{item.lastItem + item.batchSize <=
item.itemCount}"/>
|
This commandLink action
attribute
references the ItemController
backing bean next()
method which calculates
the
next page's first row number and returns a logical outcome
String, which causes the List.jsp page
to display the next page's
list .
The ItemController
next
method is defined as shown below:
Code Sample from: ItemController.java |
public String next() {
if (firstItem + batchSize
< getItemCount()) {
firstItem += batchSize;
}
return "item_list";
}
|
The JavaServer Faces NavigationHandler
matches the logical outcome, item_list
against the navigation rules in the application configuration resource
file faces-config.xml
to determine which page to access next. In this case, the
JavaServer Faces implementation loads the List.jsp
page after this method returns.
| Code Sample from: faces-config.xml |
<navigation-rule>
<navigation-case>
<from-outcome>item_list</from-outcome>
<to-view-id>/item/List.jsp</to-view-id>
</navigation-case>
</navigation-rule>
|
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 :
| Code Sample from: List.jsp |
<h:commandLink
action="#{item.prev}"
value="Previous
#{item.batchSize}"
rendered="#{item.firstItem >=item.batchSize}"/>
|
This ItemController
prev()
method is defined as shown
below:
Code Sample from: ItemController.java |
public String prev()
{
firstItem -= batchSize;
if (firstItem < 0) {
firstItem = 0;
}
return "item_list";
}
|
A JSF ommandLink
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:
| Code Sample from: List.jsp |
<h:column>
<f:facet name="header">
<h:outputText
value="Name"/>
</f:facet>
<h:commandLink action="#{item.detailSetup}"
value="#{dataTableItem.name}"/>
</h:column>
|
The ItemController
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 :
Code Sample from: ItemController.java
|
public String detailSetup()
{
item = (Item)
model.getRowData();
return "item_detail";
}
|
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.
| Code Sample from: faces-config.xml |
<navigation-rule>
<navigation-case>
<from-outcome>item_detail</from-outcome>
<to-view-id>/item/Detail.jsp</to-view-id>
</navigation-case>
</navigation-rule>
|
The Detail.jsp uses the outputText
component to display the ItemController
ManagedBean's item
properties:
| Code Sample from: detail.jsp |
<h:outputText
value="#{item.item.name}"
title="Name" />
<h:outputText value="#{item.item.description}"
title="Description"/>
<h:graphicImage url="#{item.item.imageurl}" title="Imageurl" />
<h:outputText value="#{item.item.price}" title="Price" />
<h:outputText value="#{item.item.address.city}" title="Address" />
<h:outputText value="#{item.item.contactinfo.email}"
title="Address"/>
|
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 Catalog
methods which use the
Java
Persistence APIs with Spring 2.0.
Continue Reading...
Sample Store Catalog Application using Visual Web Pack and the Java Persistence APIs
Posted by caroljmcdonald on June 04, 2007 at 10:06 AM | Permalink
| Comments (6)
Creating a new
Project
From NetBeans IDE, Create a new Visual Web Pack NetBeans project:
- Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
- Under Choose Project
pane,
select Web under Categories and Visual Web Application
under Projects, and click Next
- the Name and
Location
pane appears.
- Enter the project name vwp-sessionpagination.
- For the Project Location
field, browse to select where you want to put your project.
- Click Finish
Adding
scrolling to the application using the Java Persistence APIs
The steps below explain how to
- access a
database with the Java Persistence API.
- develop a JSP page which uses a Property Sheet
component
- bind data
objects to JavaServer Faces components to
dynamically show the details of the catalog item on the page.
Creating the Database tables
Choose Tools > Java DB Database > Start
Java DB
Server.
You'll see in the Output window indicating the server has been
started.
Unzip the dowloaded source code example. Copy the directory
vwp-sessionpagination\setup (and
all of its contents) from the
example into your project directory (the build.xml uses your project's
netbeans properties files). From a command line
window cd
into the directory yourprojectname\setup.
Then on the
command
line type: ant create-db. This will create the database
tables. The
text file with the SQL is in the directory:
vwp-sessionpagination\setup\sql\javadb .
Creating
the
Entity Classes and Persistence Unit
Create the JPA entity
classes from the data base tables.
Right-click the project node and choose New
>
File/Folder.
From the Persistence category, select Entity Classes
from
Database and click Next.
In the Data Source drop-down, select the jdbc/PETCatalogDB
database. If
prompted for a user name and password, use app and app. Select the
ITEM
table from the list, click Add and
then click Next.
Leave the tables and class names as their default, and set
the package
to model .
Click the Create Persistence Unit button. You can
leave the default the persistence unit name (Remember this name, it
will be used later to get the EntityManagerFactory) . If you installed
another persistence
provider you can select that, otherwise use TopLink. Select None
as the table generation strategy. Click
Create.
Click Finish in the Entity
Classes from
Database wizard. The IDE
creates the persistence unit (persistence.xml in the
projectname\src\conf directory) and the entity
classes (in Source Packages ).
In the Projects window expand Source Packages > data to
look at the
Item.java Entity Class which was
generated from the item table.
Adding
code
to the Session Bean to get a list of items
In the Projects window,Double-click the
SessionBean1 node
to open
the SessionBean1.java source file in the editor. Add the following code
(shown in bold)
to the code already in SessionBean1. Make sure the
PersistenceUnit unitName (shown in Red below) is the same name you used
when
creating the Persistence Unit above. This is JPA code to get and
page through a list of Items which
we will use for
scrolling through the catalog.This JPA code calls the EntityManager
createQuery and Query
getResultList methods to get a list of items and stores the results in
the SessionBean1 items property. The next(), prev()... methods will be
used by the page button action methods to scroll through
the items list.
| Code Sample : SessionBean1 |
private Item item; private int firstItem = 0; private int size=0; private int lastBatchItem=0; private int batchIndex=0; private int index=0; private List items=null; private int batchSize = 10; @PersistenceUnit(unitName = "vwp-sessionpaginationPU") private EntityManagerFactory emf; private EntityManager getEntityManager() { return emf.createEntityManager(); } public Item getItem() { return item; } public void setItem(Item item) { this.item = item; } public Item findItem(String id) { EntityManager em = getEntityManager(); try{ item = (Item) em.find(Item.class, id); return item; } finally { em.close(); } }
public int getIndex() { return this.index; } public void setIndex(int index) { this.index = index; } public Item getItem(int index){ items=getItems(); if( index >= firstItem) batchIndex= index-firstItem; else if( firstItem < batchSize){ batchIndex= firstItem; } if (batchIndex>=batchSize) batchIndex=batchSize-1; item=items.get(batchIndex); return item; } public List getItems(){ if (items==null || items.isEmpty() ){ items = getItems( firstItem,batchSize); } return this.items; } public List getNextItems() { if (size > batchSize) { items = getItems( firstItem,batchSize); } return this.items; } public List getItems(int firstItem,int batchSize) { EntityManager em = getEntityManager(); try{ Query q = em.createQuery("select object(o) from Item as o"); q.setMaxResults(batchSize); q.setFirstResult(firstItem); items=q.getResultList(); return items; } finally { em.close(); } } public int getItemCount() { EntityManager em = getEntityManager(); try{ int count = ((Long) em.createQuery("select count(o) from Item as o").getSingleResult()).intValue(); return count; } finally { em.close(); } } public void setItem(int index) { item=getItem(index); } public int getFirstItem() { index=firstItem; return firstItem; } public void first() { firstItem=0; index=firstItem; getNextItems(); } public void last() { firstItem=size-batchSize; index=size-1; getNextItems(); } public int getLastBatchItem() { lastBatchItem= firstItem + batchSize > size ? size : firstItem + batchSize; lastBatchItem--; return lastBatchItem; } public int getBatchSize() { return batchSize; } public void nextBatch() { if (firstItem + batchSize < getItemCount()) { firstItem += batchSize; index=firstItem; getNextItems(); } else{ index=getLastBatchItem(); } } public void prevBatch() { firstItem -= batchSize; if (firstItem < 0) { firstItem = 0; } index=firstItem; getNextItems(); } public void next() { if (index < size) { index++; } if (index > firstItem + batchSize) { nextBatch(); } } public void prev() { if (index>0){ index--; } if (index < firstItem) { prevBatch(); } }
|
Add the following code
in bold to the SessionBean1 init() method :
| Code Sample : SessionBean1 init() method |
public void init() { . . . items=getItems(); size = getItemCount(); if (size < batchSize){ batchSize=size; } setItem(firstItem);
}
|
Right-click in the source and choose Fix
Imports from the
pop-up
menu.
The Fix Imports dialog appears.
Select the following fully qualified
names, and click OK.
| Class Name |
Fully Qualified Name |
Query |
javax.persistence.Query |
List |
java.util.List |
Close and save the file.
Close and reopen the project.
You must close and reopen the project to make the Session Bean Item
appear in the list of Bind
to Data Objects, which we will do later with the Property components.
Add a
property
sheet and Properties to the Page1
In the Projects window, double-click Web Pages
> Page1.jsp
to open the page in the Visual Designer.
From the Layout section of the Palette,
drag a Property
Sheet component onto the page.
The Property Sheet component provides a
container for laying out the catalog item information. The Property
Sheet component contains a Property
Sheet Section, which in turn contains a Property component.
Select Property Sheet Section 1. In the Properties
window, set the label property to Pet Catalog.
In the Outline window, expand
propertySheet1 >
section1 and then select the property1 node. In the Properties window,
set the label property to Scroll:
and press Enter.
In the Outline window, select section1, right-click, and
choose
Add Property from the pop-up menu. In the Properties window, set the label
property to Name:
In the Outline window, select section1, right-click, and
choose
Add Property from the pop-up menu. In the Properties window, set the label
property to Description:
In the Outline window, select section1, right-click, and
choose
Add Property from the pop-up menu. In the Properties window, set the label
property to Price: and press Enter.
In the Outline window, select section1, right-click, and
choose
Add Property from the pop-up menu. In the Properties window, set the label
property to Photo: and press Enter.
Your Page1 page should now look like the figure below:
Drag a Button component from the
Palette and drop
it on
the property1 node in the Outline window.
The Button becomes a subnode of
property1. The Button also appears in
the Visual Designer. In the Button properties window (or on the button
itself) type << for the text property .
Drag another Button component
from the Palette and
drop
it on the property1 node in the Outline window.
In the Button properties window type
< for the
text property .
Drag another Button component
from the Palette and
drop
it on the property1 node in the Outline window.
In the Button properties window type
> for the
text property .
Drag another Button component
from the Palette and
drop
it on the property1 node in the Outline window.
In the Button properties window type >>
for the
text property.
Your Page1 page should now look like the figure below.

Drag a Static Text component from
the Palette and
drop
it on the property2 node in the Outline window.
The Static Text becomes a subnode of
property2. The Static Text also
appears in the Visual Designer. In the static text properties window
set the id property to name .
Right-click the Static Text component and
choose Bind to
Data
from the pop-up menu.
In the dialog box, In the Bind to
an Object tab,
expand SessionBean1,
expand item, select property itemname and click OK.
This will set the label text property to #{SessionBean1.item.itemname}
, as shown in the figure below:
For the Description and Price properties repeat the steps
above
to drag a static text component onto the associated property, then bind
to the associated SessionBean1 item properties:
#{SessionBean1.item.description}, #{SessionBean1.item.price}
For the Photo property drag an Image component
from the
Palette and drop it on the property4 node in the Outline window. In the
image Properties Window enter photoid
property, enter 150 for the width property, enter 150
for the height property.
Right-click the Image component and
choose Bind to Data
from the pop-up menu.
In the dialog box, In the Bind to
an Object tab,
expand
SessionBean1, expand item, select property imageurl and click
OK.
This will set the image url property to #{SessionBean1.item.imageurl}.
Go to the Page1 in the Visual Designer.
Your Page1 should now look like the figure below (I added a image of a
parrot and a Label "Java Pet Store" to the page below):
Add
Actions
to the scrolling buttons on Page1:
In the Projects window, double-click Web Pages
> Page1.jsp
to open the page in the Visual Designer.
Double-click the << button component that you just
added
to
the page.
The IDE adds a button1_action() method for processing a click on the
button and displays the button1_action() method in the source editor.
The IDE also registers the method as a handler for the action event.
Next you add code to this method.
Add the following code (shown in bold)
to the
button1_action() method. This code calls the SessionBean1.prevBatch()
method to query to get the previous batch (resultList) of items (based
on the batchSize query variable) from the item table. The
items table in this sample is not very large, but for large databases
it is useful to set a size for the query result list .
| Code Sample : button1_action() |
public String button1_action() { getSessionBean1().prevBatch(); return null; }
|
Double-click on the < button to
open the
button2_action()
method in the Java editor.
Add the following code (shown in bold)
to the
button2_action() method. This code calls the SessionBean1.prev() method
to decrement the item list index .
| Code Sample : button2_action() |
public String button2_action() { getSessionBean1().prev(); return null; }
|
Double-click on the > button to open the
button3_action()
method in the Java editor.
Add the following code (shown in bold)
to the
button3_action() method. This code calls the SessionBean1.next() method
to increment the item list index .
| Code Sample : button3_action() |
public String button3_action() { getSessionBean1().next(); return null; }
|
Double-click on the >> button to open the
button4_action()
method in the Java editor.
Add the following code (shown in bold)
to the
button4_action() method. This code calls the SessionBean1.nextBatch()
method to query to get the next batch (resultList) of items (based on
the batchSize query variable) from
the item table.
| Code Sample : button4_action() |
public String button4_action() { getSessionBean1().nextBatch(); return null; }
|
In the Java Editor for the Page1
page, scroll
up to
the prerender() method. Add the following code
(shown in bold) to the
prerender() method.
| Code Sample : prerender() |
public void prerender() { getSessionBean1().setItem(getSessionBean1().getIndex()); }
|
Run
the
project to test scrolling through the Page1 Pages using
Hibernate with the Java Persistence API
Now you are ready to build and run the project.
In the main toolbar, click Run Main
Project to run the
project.
The detailed item information for the 1st item from the ITEM database
table should be
displayed. Click on the << (Previous Batch), < (Previous),
> (Next), and >> (Next Batch)
buttons
to
scroll through the items, from the ITEM database table, displayed on
the page.
Summary:
We went over how to develop a quick application that
accesses a database with the Java Persistence API
and how to configure and use the Property Sheet, Property, Static Text,
and Button components in NetBeans Visual Web Pack
to display and scroll through dynamic data from the database.
|