Sample Application using JSF, Seam, and Java Persistence APIs on Glassfish
This Sample Store Catalog app demonstrates the usage of JavaServer
Faces, a Catalog Stateful Session Bean, the Java Persistence APIs, and
Seam to implement
pagination of data sets. I took this example
href="http://weblogs.java.net/blog/caroljmcdonald/archive/2007/05/pagination_of_d.html">Pagination
of Data Sets in a Sample Application using JSF, Catalog Facade
Stateless Session, and Java Persistence APIs and refactored
it to use Seam on Glassfish by following the steps in Brian Leonards
blog
href="http://weblogs.java.net/blog/bleonard/archive/2007/06/seam_refresh_1.html">
href="http://weblogs.java.net/blog/bleonard/archive/2007/06/seam_refresh_1.html">Seam
Refresh and the clickable list example in the
href="http://docs.jboss.com/seam/1.0.1.GA/reference/en/html/tutorial.html">Seam
Tutorial.
href="https://techdayscode.dev.java.net/servlets/ProjectDocumentList?folderID=7555">Download
the Seam Sample Application Code
Explanation of the usage of JSF, Seam, 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.
DataTable JSF component
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:
style="color: rgb(204, 0, 0); font-weight: bold;">Red colors
are for Java EE
tags, annotations code,
style="color: rgb(0, 0, 153); font-weight: bold;">Blue for
style="color: rgb(0, 0, 153); font-weight: bold;">Seam specific
and Green
for my code
or variables)
Code Sample from: |
|
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
style="color: rgb(0, 102, 0); font-weight: bold;">items style="font-weight: bold;">. The varattribute points
to a single item in that list. As the
UIDatacomponent iterates through the list, each reference to
dataTableItem
style="font-weight: bold;"> points to the current item in thelist.
The dataTable's
style="font-weight: bold; color: rgb(204, 0, 0);">valueis bound to the
style="font-weight: bold; color: rgb(0, 102, 0);">itemsattribute
of the
CatalogBeanclass:
Code Sample from: .java |
|
The @DataModel
Seam annotation exposes an attribute of type java.util.List to a JSF
page as an instance of
style="color: rgb(204, 0, 0); font-weight: bold;">javax.faces.model.DataModel.
The
<
style="font-weight: bold; color: rgb(204, 0, 0);">h:dataTable style="color: rgb(204, 0, 0);">
style="color: rgb(204, 0, 0); font-weight: bold;">>
style="font-weight: bold;"> supports data binding to acollection 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. In this case, the DataModel is made available
in a session
context variable named style="font-weight: bold; color: rgb(0, 102, 0);">items.
When the List.jsp page is diplayed it will try to resolve the style="font-weight: bold; color: rgb(0, 102, 0);" class="literal">items
context variable. Since this context variable is not initialized, Seam
will call the factory method style="font-weight: bold; color: rgb(0, 102, 0);">getItems(),
which performs a JPA query (see
style="color: rgb(0, 0, 0);">getNextItems() code below)and results in a DataModel being outjected.
style="font-weight: bold; color: rgb(0, 0, 153);"> SeamThestyle="font-weight: bold; color: rgb(0, 0, 153);">@Name
annotation specifies
style="color: rgb(0, 102, 0); font-weight: bold;">catalog asthe application unique component name which Seam will use to resolve
references to the
style="color: rgb(0, 102, 0); font-weight: bold;">catalogcontext variable. Seam will instantiate the
component
and bind a new instance to the context variable the first time JSF
encounters the variable name
style="color: rgb(0, 102, 0); font-weight: bold;">catalog.The instance will be bound to the
context specified by the
style="font-weight: bold; color: rgb(0, 0, 153);">@ScopeSeam annotation. The style="font-family: monospace; color: rgb(0, 102, 0); font-weight: bold;">CatalogBean
is a
org.jboss.seam.ScopeType.SESSIONscoped component.
The style="color: rgb(204, 0, 0); font-weight: bold;"> style="color: rgb(204, 0, 0);">@Stateful EJB 3.0annotation marks this as a Stateful EJB. A Stateful EJB is used because
the current chunk of items, and
the user's position in the count of items in the db table, is
maintained for the user's session.
The
style="color: rgb(204, 0, 0);">@Interceptors EJB3.0 annotation registers the style="color: rgb(0, 0, 153);">SeamInterceptor.class as
an EJB interceptor for this session bean component.
The Seam framework uses href="http://java.sun.com/mailers/techtips/enterprise/2006/TechTips_Oct06.html#2">EJB
interceptors to perform bijection, context demarcation, validation,
etc, (the interceptor could be defined in the style="color: rgb(204, 0, 0); font-weight: bold;" class="literal">ejb-jar.xml
instead).
Column JSF component
On the List.jsp page the Item Name, Photo, and Price properties
are displayed with the
style="color: rgb(204, 0, 0);">column component:
Code Sample from: |
|
The column tags represent columns of data in the dataTable component.
While
the dataTable component is iterating over the rows of data, it
processes
the column component associated with each
style="color: rgb(204, 0, 0);">column tag for each row in
the table. As the dataTable
component iterates through the list, each reference to dataTableItem
style="font-weight: bold;"> points to the
style="font-weight: bold;">current item in the
list.
The dataTable component iterates through the list of items and
displays the names, photos, and prices. Each
time the dataTable 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
style="color: rgb(204, 0, 0);">headers, footers,
and captions.
Java Persistence Query API
The CatalogBean
Session EJB uses the Java Persistence API
style="color: rgb(204, 0, 0);">
style="color: rgb(204, 0, 0);"> style="font-weight: bold; color: rgb(204, 0, 0);">Query style="color: rgb(204, 0, 0); font-weight: bold;"> objectto return a list of
style="font-weight: bold; color: rgb(0, 102, 0);">items.With the
style="color: rgb(204, 0, 0);">@PersistenceContext annotationthe CatalogBean uses dependency injection to lookup and obtain a
Container Managed
style="font-weight: bold;">EntityManager
style="color: rgb(204, 0, 0);">
style="font-weight: bold; color: rgb(204, 0, 0);">
style="color: rgb(204, 0, 0);">
style="font-weight: bold;">
style="color: rgb(204, 0, 0);"> style="font-weight: bold; color: rgb(204, 0, 0);">.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).
Code Sample from: .java |
|
Since this query is used for Read-Only browsing, the
style="color: rgb(204, 0, 0);">transaction
attribute in this example is specified as
style="color: rgb(204, 0, 0);">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 Java Persistence
href="http://java.sun.com/javaee/5/docs/api/javax/persistence/Query.html">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: query.
style="color: rgb(204, 0, 0);">setMaxResults(int maxResult)
sets the maximum number of results to retrieve.
style="font-family: mon;"> query.
style="color: rgb(204, 0, 0);">setFirstResult(int startPosition)
sets the position of the first result to retrieve.
In the code below, we show the
style="color: rgb(0, 102, 0); font-weight: bold;">Itementity 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
style="color: rgb(204, 0, 0); font-weight: bold;">@Entity
annotation.
- annotating the primary key identifier with
style="color: rgb(204, 0, 0); font-weight: bold;">@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
style="color: rgb(0, 102, 0); font-weight: bold;">Addressand
Productare also annotated. For more information on
defining JPA entities see href="http://www.apress.com/book/bookDisplay.html?bID=10093">Pro
EJB 3: Java Persistence API book.
Code Sample from: .java |
@Id
|
The @Name
href="http://docs.jboss.com/seam/1.0.1.GA/reference/en/html/annotations.html#name-annotation">
style="font-weight: bold; color: rgb(0, 0, 153);">seam annotation specifies the (application unique) class="emphasis">component name
style="color: rgb(0, 102, 0); font-weight: bold;">item,which is used in
the
style="color: rgb(0, 102, 0); font-weight: bold;">Detail style="color: rgb(0, 0, 0);">.jsp
style="font-weight: bold;"> to display the selected item'sattributes. The
style="font-weight: bold; color: rgb(0, 0, 153);">@ScopeSeam annotation binds the
style="color: rgb(0, 102, 0); font-weight: bold;">iteminstance to the
org.jboss.seam.ScopeType.EVENTcontext.
The
CatalogBeanpages through the list of
style="color: rgb(0, 102, 0); font-weight: bold;">itemsby
maintaining the
style="color: rgb(0, 102, 0); font-weight: bold;">firstItemand
batchSizeattributes and passing these as
parameters to the style="color: rgb(204, 0, 0);"> query. style="color: rgb(204, 0, 0);">setFirstResult(int startPosition), style="font-weight: bold; color: rgb(204, 0, 0);"> query. style="color: rgb(204, 0, 0);">setMaxResults(int maxResult)
methods.
The CatalogBean's scope is defined as style="color: rgb(0, 0, 153);">org.jboss.seam.ScopeType.SESSION,
which corresponds to the JSF style="color: rgb(0, 0, 0);">managed bean session scope.
The
CatalogBeanitemCountproperty is used to get and display
the number of Catologue items in the data base:
Code Sample from: |
|
The CatalogBean
getItemCount()
method uses the JPA javax.persistence.
style="font-weight: bold; color: rgb(204, 0, 0);">Queryinterface to get the count of
all items in the database item table:
Code Sample from: .java |
itemCount |
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
style="color: rgb(204, 0, 0); font-weight: bold;">actionevent to the application.
Code Sample from: |
|
This
commandLink style="color: rgb(204, 0, 0); font-weight: bold;">action attributereferences the CatalogBean
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
CatalogBeannext()method is defined as shown below:
Code Sample from: .java |
|
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 |
|
A JSF commandLink
is used to provide a link to click on to
display the previous page of items. This
style="color: rgb(204, 0, 0);">commandLink
action attribute references the
style="color: rgb(0, 102, 0); font-weight: bold;">CatalogBean's 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: |
|
This
style="color: rgb(0, 102, 0); font-weight: bold;">CatalogBeanprev() method is defined as shown
below:
Code Sample from: .java |
|
A JSF commandLink is used to provide a link to click on to
display a page with the item details. This
style="color: rgb(204, 0, 0); font-weight: bold;">commandLink
action attribute references the
style="color: rgb(0, 102, 0); font-weight: bold;">CatalogBean select()method:
Code Sample from: |
|
With Seam if you use the @DataModelSelection with the @DataModel
annotation, when the user clicks on the link, Seam will propagate the
selected row from the
style="font-weight: bold; color: rgb(0, 102, 0);"> style="font-family: monospace;">
style="color: rgb(204, 0, 0);" class="literal">DataModel
style="color: rgb(204, 0, 0);" class="literal"> into theannotated attribute:
Code Sample from: .java |
|
The @DataModelSelection
Seam annotation tells Seam to inject the
style="color: rgb(204, 0, 0); font-weight: bold;">DataModel
List
element corresponding to the clicked link into the
style="color: rgb(0, 102, 0); font-weight: bold;"> style="color: rgb(0, 102, 0); font-weight: bold;">item attribute. The
@OutSeam annotation transfers the value of this attribute to the
style="color: rgb(0, 102, 0); font-weight: bold;">itemevent context
variable, making it available to a
JSP page after the
style="color: rgb(204, 0, 0); font-weight: bold;">actioncatalog.select
method execution. So when a row of thedataTable is selected, the
selected row
is injected to the
style="color: rgb(0, 102, 0); font-weight: bold;">itemattribute of the
style="color: rgb(0, 102, 0); font-weight: bold;">CatalogBean
style="color: rgb(204, 0, 0);">Stateful bean, and thenoutjected to the event context
variable named
style="color: rgb(0, 102, 0); font-weight: bold;">itemwhich is used in the
Detail style="color: rgb(0, 0, 0);">.jsp page to displaythe item details.
The
CatalogBean
select() returns a string, style="font-weight: bold; color: rgb(0, 102, 0);">"item_detail", whichcauses the
Detail style="color: rgb(0, 0, 0);">.jsp page to displaythe item details. The JavaServer Faces
NavigationHandlermatches the logical outcome,
style="font-weight: bold; color: rgb(0, 102, 0);">item_detail
against the navigation rules in the application configuration resource
file faces-config.xml style="color: rgb(204, 0, 0);"> to determine which page to
access next. In this case, the
JavaServer Faces implementation loads thestyle="color: rgb(0, 102, 0); font-weight: bold;">Detail style="color: rgb(0, 0, 0);">.jsp
page after this method returns.
| Code Sample from: faces-config.xml |
|
The Detail.jsp uses the
style="color: rgb(204, 0, 0); font-weight: bold;">outputTextcomponent to display the
style="color: rgb(0, 102, 0); font-weight: bold;">itemproperties:
Code Sample from: |
|
src="http://weblogs.java.net/blog/caroljmcdonald/archive/images/detailpage.jpg"
height="800" width="646">
Conclusion
This concludes the sample application which demonstrates how to use
style="color: rgb(0, 0, 153);">Seam
with the JSF
style="color: rgb(204, 0, 0);">dataTable and
style="color: rgb(204, 0, 0);">DataModel to page through a
list
of Item
style="color: rgb(204, 0, 0);">Entities which are retrieved
using the
CatalogBean
style="color: rgb(204, 0, 0);">Stateful Session EJB methods
which use the
Java
Persistence APIs.
Configuration of the Application
for Seam, JSF, JPA, running on Glassfish
First I recommend reading and following the steps in Brian Leonard's
blog
href="http://weblogs.java.net/blog/bleonard/archive/2007/06/seam_refresh_1.html">Seam
Refresh . I will summarize
those steps here:
- Download
and extract JBoss 4.2.0 GA -
href="https://glassfish.dev.java.net/public/downloadsindex.html">Download
and install GlassFish V1, following the instructions on the download
page. Alternatively you can use Sun Java System Application Server PE
9, Sun's GlassFish distribution. - Download
and extract Seam 1.2.1 GA - Download
and install NetBeans 5.5.1 -
href="https://techdayscode.dev.java.net/servlets/ProjectDocumentList?folderID=7555">Download
the Seam Sample Application Code - install Glassfish and Netbeans 5.5.1. Then add the
glassfish application server to Netbeans.
To Open and Test Run the seampagination Project:
- Use the Resolve Reference Problems dialog to map the ejb and web
modules to their
project, which are subdirectories beneath the seampagination
directory. - After the references are resolved, right-click the seampagination
project and select Open Required Projects. - In Netbeans create a JBossSeam Library as documented in
href="http://weblogs.java.net/blog/bleonard/archive/2007/06/seam_refresh_1.html">Seam
Refresh. - Right-click the seampagination-EJBModule to resolve the
reference problems:
- browse to the server\default\deploy\jboss-web.deployer\jsf-libs
directory of your JBoss server installation, select jsf-api.jar and
select Open.
- browse to the Seam lib directory and select hibernate-all.jar
and thirdparty-all.jar, and
select Open. - Right-click the Libraries node of the seampagination-EJBModule
project , choose Add Library and add the JBossSeam library you created. - Right-click the seampagination-WebModule and select Resolve
Reference
Problems: - Browse to the seampagination-ejb directory which is a
sub-directory below the seampagination directory and select Open
Project
Folder. - Browse to the jboss-seam-ui.jar found in the root of
where you extracted the Seam framework and select Open Project
Folder.
If you want to create your own Java EE
application using Seam on Glassfish with Netbeans from scratch
(read the steps in Brian Leonard's
blog
href="http://weblogs.java.net/blog/bleonard/archive/2007/06/seam_refresh_1.html">Seam
Refresh summarized here):
- In Netbeans create a JBossSeam Library as documented in
href="http://weblogs.java.net/blog/bleonard/archive/2007/06/seam_refresh_1.html">Seam
Refresh. - Use Netbeans to create a new Enterprise Application
- Right-click the Libraries node of the EJBModule project , choose
Add Library and add the JBossSeam library you created. - Right-click the Libraries node of the EJBModule project , choose
Add Jar and add these jars: - jboss
\server\default\deploy\jboss-web.deployer\jsf-libs\jsf-api.jar - jboss
\server\default\deploy\jboss-web.deployer\jsf-libs\jsf-impl.jar - Seam \lib\hibernate-all.jar
- Seam \lib\thirdparty-all.jar
- Right-click the Libraries node of the WebModule project ,
choose Add Jar and add these jars: - your ejbModule
- Seam jboss-seam-ui.jar
- create an empty seam.properties file in the
seampagination-EJBModule src\conf Folder. - add the following phase listener to your faces-config.xml
file under webpages web-inf:
<lifecycle class="xml-tag">>
< style="color: rgb(204, 0, 0);">phase-listener class="xml-tag">>
style="color: rgb(0, 0, 153);">org.jboss.seam.jsf.SeamPhaseListener
class="xml-tag"></phase-listener>
class="xml-tag"></lifecycle>
class="xml-tag"> - add the following context parameter
to your web.xml file
<context-param class="xml-tag">>
< style="color: rgb(204, 0, 0);">param-name class="xml-tag">>
style="color: rgb(0, 0, 153);"> org.jboss.seam.core.init.jndiPattern
class="xml-tag"></param-name class="xml-tag">>
< style="color: rgb(204, 0, 0);">param-value class="xml-tag">>
java:comp/env/ style="font-style: italic; color: rgb(0, 102, 0);">your ear name/#{ejbName}/local
class="xml-tag"></param-value class="xml-tag">>
class="xml-tag"></context-param> - add the following listener class to your
web.xml file
<listener class="xml-tag">>
< style="color: rgb(204, 0, 0);">listener-class class="xml-tag">>
style="color: rgb(0, 0, 153);">org.jboss.seam.servlet.SeamListener
class="xml-tag"></listener-class class="xml-tag">>
</ class="xml-tag">listener class="xml-tag">>
- For any session EJB's referenced from the web, add EJB
references to your web.xml, for example:
<ejb-local-ref class="xml-tag">>
< style="color: rgb(204, 0, 0);">ejb-ref-name class="xml-tag">> style="font-style: italic; color: rgb(0, 102, 0);">your ear name/CatalogBean/local class="xml-tag"></ejb-ref-name> class="xml-tag">
< style="color: rgb(204, 0, 0);">ejb-ref-type class="xml-tag">>Session</ejb-ref-type> class="xml-tag"> class="xml-tag"> class="xml-tag"> class="xml-tag">
< style="color: rgb(204, 0, 0);">local-home/ class="xml-tag">> class="xml-tag"> class="xml-tag">
< style="color: rgb(204, 0, 0);">local> style="font-style: italic; color: rgb(0, 102, 0);">your package name.Catalog class="xml-tag"></local> class="xml-tag">
< style="color: rgb(204, 0, 0);">ejb-link>CatalogBean class="xml-tag"></ejb-link> class="xml-tag">
</ class="xml-tag">ejb-local-ref> - For any EJB's referenced from the web add a Seam
interceptor to
the EJB, for example :style="font-weight: bold; color: rgb(204, 0, 0);">@Interceptors({ style="color: rgb(0, 0, 153);">org.jboss.seam.ejb.SeamInterceptor.class})
References:
-
href="http://weblogs.java.net/blog/bleonard/archive/2007/06/seam_refresh_1.html">Seam
Refresh -
href="http://docs.jboss.com/seam/1.0.1.GA/reference/en/html/tutorial.html">Seam
Tutorial -
href="http://www.michaelyuan.com/blog/2007/02/28/glassfish-and-seam-tips/">Glassfish
and Seam Tips -
href="http://www.amazon.com/exec/obidos/ASIN/0131347969/mobileenterpr-20/">JBoss
Seam: Simplicity and Power Beyond Java book
-
href="https://blueprints.dev.java.net/bpcatalog/ee5/persistence/index.html">Java
BluePrints Solutions Catalog for the Java Persistence APIs
contains a collection of topics and example applications. -
target="bpcatalog">Java
Persistence reference page on GlassFish Project - Java
EE tutorial, for good tutorial on JSF and JPA - Pro
EJB 3: Java Persistence API book
target="bpcatalog">
Carol McDonald
- Login or register to post comments
- Printer-friendly version
- caroljmcdonald's blog
- 30710 reads





