Skip to main content

Sample Application using JSF, Seam 2.0, and Java Persistence APIs on Glassfish V2

Posted by caroljmcdonald on January 27, 2008 at 7:58 PM PST

Sample Application using JSF, Seam 2.0,
and Java Persistence APIs on Glassfish V2



This Sample Store Catalog app demonstrates the usage of JavaServer
Faces, a Catalog Stateful Session Bean, the Java Persistence APIs, and
Seam 2. I took this example  href="http://weblogs.java.net/blog/caroljmcdonald/archive/2007/05/pagination_of_d.html">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">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.


listpage.jpg src="http://weblogs.java.net/blog/caroljmcdonald/archive/images/listpage.jpg">


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: 
List style="color: rgb(0, 0, 0);">.jsp


<h:dataTable style="color: rgb(204, 0, 0);"> style="font-weight: bold; color: rgb(204, 0, 0);">value=' style="font-weight: bold;">#{ style="color: rgb(0, 102, 0); font-weight: bold;">items style="font-weight: bold;">}' style="font-weight: bold; color: rgb(204, 0, 0);">var=' style="color: rgb(0, 102, 0); font-weight: bold;">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

 style="color: rgb(0, 102, 0); font-weight: bold;">items style="font-weight: bold;">
. The var
attribute points
to a single item in that list. As the UIData
component iterates through the list, each reference to dataTableItem style="font-weight: bold;"> points to the current item in the
list.



The dataTable's
 style="font-weight: bold; color: rgb(204, 0, 0);">value

is bound to the
 style="font-weight: bold; color: rgb(0, 102, 0);">items

attribute
of the CatalogBean
class:




Code Sample from:
 style="color: rgb(0, 102, 0); font-weight: bold;">CatalogBean
.java
 style="font-weight: bold; color: rgb(204, 0, 0);">

@Stateful

      @Scope style="color: rgb(0, 0, 153);">(SESSION)

      @Name(" style="color: rgb(0, 102, 0); font-weight: bold;">catalog")

      @Interceptors({org.jboss.seam.ejb.SeamInterceptor.class})

     

public class CatalogBean
implements Serializable, style="color: rgb(0, 102, 0); font-weight: bold;">Catalog {

   

    style="font-weight: bold; color: rgb(0, 0, 153);">@DataModel

    private List style="font-weight: bold; color: rgb(0, 102, 0);">items=null;

     

    style="font-weight: bold; color: rgb(0, 0, 153);">@Factory(" style="color: rgb(0, 102, 0); font-weight: bold;">items")

    public void style="font-weight: bold; color: rgb(0, 102, 0);">getItems() {

        if ((items==null)  ||
(index != firstItem) ){

            style="color: rgb(0, 0, 0);">getNextItems();

        }

    }

   

     

     





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 a
collection of data objects represented by a style="color: rgb(204, 0, 0); font-weight: bold;">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
 style="font-weight: bold; color: rgb(0, 0, 153);">@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.  The style="color: rgb(0, 0, 153); font-weight: bold;" class="literal">@Factory
annotation tells Seam to invoke the style="font-weight: bold; color: rgb(0, 102, 0);">getItems()
method to initialize the
 style="font-weight: bold; color: rgb(0, 102, 0);">items 
value.



The  style="font-weight: bold; color: rgb(0, 0, 153);">@Name
style="font-weight: bold; color: rgb(0, 0, 153);">  Seam
annotation specifies
 style="color: rgb(0, 102, 0); font-weight: bold;">catalog 
as
the application unique component name which Seam will use to resolve
references to the
 style="color: rgb(0, 102, 0); font-weight: bold;">catalog

context 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);">@Scope

Seam annotation. The style="font-family: monospace; color: rgb(0, 102, 0); font-weight: bold;">CatalogBean
is a org.jboss.seam.ScopeType.SESSION
scoped component. This means that the JSF components  can bind to
the catalog 
managed bean without configuring this in the faces-config.xml.



The
 style="color: rgb(204, 0, 0); font-weight: bold;"> style="color: rgb(204, 0, 0);">@Stateful 
EJB 3.0
annotation 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 
EJB
3.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:
List style="color: rgb(0, 0, 0);">.jsp
< style="font-weight: bold; color: rgb(204, 0, 0);">h:dataTable style="color: rgb(204, 0, 0);">  style="font-weight: bold; color: rgb(204, 0, 0);">value=' style="font-weight: bold;">#{ style="color: rgb(0, 102, 0); font-weight: bold;">items style="font-weight: bold;">}'  style="font-weight: bold; color: rgb(204, 0, 0);">var=' style="color: rgb(0, 102, 0); font-weight: bold;">dataTableItem'
border="1"

      cellpadding="2" cellspacing="0">

...

     
  < style="font-weight: bold; color: rgb(204, 0, 0);">h:column>

      header">

          value="Price"/>

     


      value="#{dataTableItem. style="color: rgb(0, 102, 0); font-weight: bold;">price}"/>

 


     

     



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
row in
each column.



The dataTable and column tags use facets to represent rows 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;"> 
object
to return a list of
 style="font-weight: bold; color: rgb(0, 102, 0);">items
.
With the
 style="color: rgb(204, 0, 0);">@PersistenceContext 
annotation
the 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:
 style="color: rgb(0, 102, 0); font-weight: bold;">CatalogBean
.java
 

  
 style="font-weight: bold; color: rgb(0, 0, 153);">@DataModel

   private List style="font-weight: bold; color: rgb(0, 102, 0);">items=null;

     

     
    style="font-weight: bold; color: rgb(204, 0, 0);">@PersistenceContext(unitName="PetCatalogPu")

   private style="font-weight: bold;">EntityManager em;

     

   private int style="color: rgb(0, 102, 0); font-weight: bold;">batchSize =
10;

   private int index = 0;

   private int style="color: rgb(0, 102, 0); font-weight: bold;">firstItem = 0;

 

   @TransactionAttribute style="color: rgb(204, 0, 0);">(TransactionAttributeType.NOT_SUPPORTED)

   public void style="font-weight: bold; color: rgb(0, 102, 0);">getNextItems()
{

        style="color: rgb(204, 0, 0); font-weight: bold;">Query q = style="color: rgb(204, 0, 0); font-weight: bold;">em.createQuery("select
object(o) from Item as o");

        q. style="color: rgb(204, 0, 0); font-weight: bold;">setMaxResults(batchSize);

        q. style="color: rgb(204, 0, 0); font-weight: bold;">setFirstResult(firstItem);

        style="color: rgb(0, 102, 0); font-weight: bold;">items= style="color: rgb(204, 0, 0); font-weight: bold;">q.getResultList();

        index = firstItem;

   }

   

     



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;">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:

  1. annotating the class with an style="color: rgb(204, 0, 0); font-weight: bold;">@Entity
    annotation.
  2. 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;">Address

and Product
are 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:
 style="color: rgb(0, 102, 0); font-weight: bold;">Item
.java


      @Entity

      @Name(" style="color: rgb(0, 102, 0); font-weight: bold;">item")

      @Scope( style="color: rgb(0, 0, 153);">ScopeType.EVENT)

     

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;

    style="color: rgb(204, 0, 0); font-weight: bold;">@OneToOne style="color: rgb(204, 0, 0);">(cascade={CascadeType.PERSIST})

    private style="color: rgb(0, 102, 0); font-weight: bold;">Address address;

     
     style="color: rgb(204, 0, 0); font-weight: bold;">@ManyToOne

    private style="color: rgb(0, 102, 0); font-weight: bold;">Product product;


     

    public Item() { }

    


          public String getName() {

       
return name;

    }


    public void setName(String name) {

       
this.name = name;

    }
    

 
     


     

    ...

}   

     

     





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's
attributes. The
 style="font-weight: bold; color: rgb(0, 0, 153);">@Scope

Seam annotation binds the
 style="color: rgb(0, 102, 0); font-weight: bold;">item

instance to the org.jboss.seam.ScopeType.EVENT
context.



The CatalogBean
pages through the list of
 style="color: rgb(0, 102, 0); font-weight: bold;">items

by
maintaining the
 style="color: rgb(0, 102, 0); font-weight: bold;">firstItem

and batchSize
attributes 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 CatalogBean
itemCount
property is  used to get and display
the number of Catologue items in the  data base:


Code Sample from:
List style="color: rgb(0, 0, 0);">.jsp


catalog.firstItem
+
1}..#{catalog.lastItem}
of

    
#{catalog.itemCount}"/>

     

     





The CatalogBean
getItemCount()
method uses the JPA javax.persistence.

 style="font-weight: bold; color: rgb(204, 0, 0);">Query

interface to get the count of
all items in the database item table:


Code Sample from: 
 style="color: rgb(0, 102, 0); font-weight: bold;">CatalogBean
.java
        


   
private int  style="color: rgb(0, 102, 0); font-weight: bold;">
 style="color: rgb(0, 102, 0); font-weight: bold;">itemCount
 style="color: rgb(0, 102, 0); font-weight: bold;"> = 0;

     

     
    public int  style="color: rgb(0, 102, 0); font-weight: bold;">getItemCount()
{

        style="font-weight: bold; color: rgb(204, 0, 0);">Query q = style="color: rgb(204, 0, 0); font-weight: bold;">entityManager.createQuery("select
count(o) from Item as
o");     

       
itemCount
 =
((Long)q.getSingleResult()).intValue(); style="font-weight: bold;">     

        return
 style="color: rgb(0, 102, 0); font-weight: bold;">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 element. The commandLink
tag is used to submit an

 style="color: rgb(204, 0, 0); font-weight: bold;">action

event to the application. 


Code Sample from:
List style="color: rgb(0, 0, 0);">.jsp


 commandLink
      action="#{ style="color: rgb(0, 102, 0);">catalog. style="color: rgb(0, 102, 0); font-weight: bold;">next}"
value="Next
#{catalog.batchSize}"

   
rendered="#{catalog.lastItem + catalog.batchSize <=
catalog.itemCount}"/>   

     



This

commandLink  style="color: rgb(204, 0, 0); font-weight: bold;">action 
attribute
references 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 CatalogBean
next()
method is defined as shown below:


Code Sample from:
 style="color: rgb(0, 102, 0); font-weight: bold;">CatalogBean
.java


   public String  style="color: rgb(0, 102, 0); font-weight: bold;">next() {

       if (firstItem + batchSize
< getItemCount()) {

          
firstItem += batchSize;

          
getNextItems();

       }

       return " style="color: rgb(0, 102, 0);">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


  < style="color: rgb(204, 0, 0); font-weight: bold;">navigation-rule>

      < style="color: rgb(204, 0, 0); font-weight: bold;">navigation-case>

         
<from-outcome> style="color: rgb(0, 102, 0);">item_list

         
<to-view-id>/item/ style="color: rgb(0, 102, 0); font-weight: bold;">List.jsp
     

     

 

     

     



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:
List style="color: rgb(0, 0, 0);">.jsp


 commandLink
      action="#{ style="color: rgb(0, 102, 0); font-weight: bold;">catalog.prev}"
value="Previous
#{catalog.batchSize}"   

       
rendered="#{catalog.firstItem >=catalog.batchSize}"/>

     

     



 This

 style="color: rgb(0, 102, 0); font-weight: bold;">CatalogBean

prev() method 
is defined as shown
below: 

 

Code Sample from:
 style="color: rgb(0, 102, 0); font-weight: bold;">CatalogBean
.java


    public String  style="color: rgb(0, 102, 0);">prev()
{

        firstItem -= batchSize;

        if (firstItem < 0) {

           
firstItem = 0;

        }

        getNextItems();

        return " style="color: rgb(0, 102, 0); font-weight: bold;">item_list";

    }     

     



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:
List style="color: rgb(0, 0, 0);">.jsp


  

      

          value="Name"/>

      


       style="color: rgb(204, 0, 0); font-weight: bold;">commandLink
action="#{catalog.select}"
value="#{dataTableItem.name}"/>   

  


     



With Seam if you use the

 style="color: rgb(0, 0, 153); font-weight: bold;">@DataModelSelection
with the @DataModel
annotation, when the user clicks on the link, Seam will propagate the
selected row style="font-family: mon;">from the
 style="font-weight: bold; color: rgb(0, 102, 0);"> style="font-family: monospace;"> 
style="color: rgb(204, 0, 0); font-weight: bold;" class="literal">DataModel style="color: rgb(204, 0, 0);" class="literal"> into the
annotated attribute:


Code Sample from:
 style="color: rgb(0, 102, 0); font-weight: bold;">CatalogBean
.java


    @ style="color: rgb(0, 0, 153); font-weight: bold;">DataModelSelection

    style="color: rgb(0, 0, 153); font-weight: bold;">@Out(required=false)

    private Item style="color: rgb(0, 102, 0); font-weight: bold;">item;

     

    public String style="color: rgb(0, 102, 0); font-weight: bold;">select() {

        return " style="font-weight: bold; color: rgb(0, 102, 0);">item_detail";

    }

     





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 @Out
Seam annotation transfers the value of this attribute to the
 style="color: rgb(0, 102, 0); font-weight: bold;">item

event context
variable, making it available to a
JSP page after the
 style="color: rgb(204, 0, 0); font-weight: bold;">action

catalog.select
method execution. So when a row of the
dataTable is selected, the
selected row
is injected to the
 style="color: rgb(0, 102, 0); font-weight: bold;">item

attribute of the
 style="color: rgb(0, 102, 0); font-weight: bold;">CatalogBean 
style="color: rgb(204, 0, 0);">Stateful bean, and then
outjected to the event context
variable named
 style="color: rgb(0, 102, 0); font-weight: bold;">item

which is used in the
Detail style="color: rgb(0, 0, 0);">.jsp
page to display
the item details.



The
CatalogBean
select()
returns a string, 
 style="font-weight: bold; color: rgb(0, 102, 0);">"item_detail", 
which
causes the
Detail style="color: rgb(0, 0, 0);">.jsp
page to display
the item details. The JavaServer Faces NavigationHandler
matches 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 the style="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
 

    < style="color: rgb(204, 0, 0); font-weight: bold;">navigation-rule>

        < style="color: rgb(204, 0, 0); font-weight: bold;">navigation-case>

           
<from-outcome> style="color: rgb(0, 102, 0);">item_detail

           
<to-view-id>/item/ style="color: rgb(0, 102, 0); font-weight: bold;">Detail.jsp

       

   
   


   



The Detail.jsp uses the

 style="color: rgb(204, 0, 0); font-weight: bold;">outputText

component to display the
 style="color: rgb(0, 102, 0); font-weight: bold;">item

properties:


Code Sample from: 
 style="color: rgb(0, 102, 0); font-weight: bold;">Detail style="color: rgb(0, 0, 0);">.jsp
 

   
outputText
value="#{item.name}"
title="Name" />

   
style="color: rgb(0, 102, 0); font-weight: bold;">item
.description}"
title="Description"/>

   
item.imageurl}"
title="Imageurl" />

   
item.price}"
title="Price" />

   
item.address.city}"
title="Address" />

   
item.contactinfo.email}"
title="Address"/>
 






    alt="detailpage.jpg"
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 API
s.




Configuration of the Application
for Seam 2.0, JSF, JPA, running on Glassfish V2

First I recommend reading Brian Leonard's
blog href="http://weblogs.java.net/blog/bleonard/archive/2007/06/seam_refresh_1.html">Seam
Refresh
.  I will summarize
and update

those steps here:


  • Download
    and install NetBeans 6.1 bundled with GlassFish V2
  • Alternatively you can  href="https://glassfish.dev.java.net/public/downloadsindex.html">Download
    and install GlassFish V2 separately.
  • Download
    and extract Seam 2.0
  • href="https://techdayscode.dev.java.net/servlets/ProjectDocumentList?folderID=7555">Download
    the  Sample Application Code

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.
  • Right-click
    the seampagination-EJBModule and select Resolve
    Reference
    Problems:

    • browse to the Seam lib directory and select style="font-weight: bold;"> jboss-seam.jar
      and
      select Open. This should resove the reference to the following jars:
      jboss-seam.jar, hibernate.jar,
      hibernate-validator.jar,
      hibernate-annotations.jar, hibernate-commons-annotations.jar,
      javassist.jar, dom4j.jar, commons-logging.jar.

  • 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 Seam lib
      directory.  This should resolve the reference to the following
      jars:
      jboss-seam-ui.jar and
      jboss-el.jar
      .

If you want to create your own Java EE
application using Seam 2.0 on Glassfish V2 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 but use the SEAM 2.0 jars
listed here
here):

  • Use
    Netbeans to create a new Enterprise Application
  • Right-click the Libraries node of the EJBModule project , choose
    Add Jar  and add these jars:

    • Seam \lib\jboss-seam.jar
    • Seam \lib\hibernate.jar
    • Seam \lib\hibernate-validator.jar
    • Seam \lib\ class="style1">hibernate-annotations.jar

    • Seam \lib\ class="style1">hibernate-commons-annotations.jar
    • Seam \lib\javassist.jar
    • Seam \lib\dom4j.jar
    • Seam \lib\commons-logging.jar
  • Right-click the Libraries node of the WebModule project , 
    choose Add Jar  and add these jars:

    • your ejbModule
    • Seam \lib\jboss-seam-ui.jar
    • Seam \lib\jboss-el.jar
  • create an empty seam.properties file in the
    seampagination-EJBModule src\conf Folder.
  • add  the following phase listener to your style="font-weight: bold;">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">>
    class="xml-tag">>
    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"> class="xml-tag">>   
    class="xml-tag">>
  • 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">> class="xml-tag">
         < style="color: rgb(204, 0, 0);">ejb-ref-type class="xml-tag">>Session> 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">> class="xml-tag">
         < style="color: rgb(204, 0, 0);">ejb-link>CatalogBean class="xml-tag">> 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:






Related Topics >>

Comments

I've deployed this seam 2.0 with jsf application but I don't know the URL to test the page, can you please let me know the URL to test this application?

I've deployed this seam application but don't know the URL to access it. Can you please let me know the URL to access this application.

I haven't tried seam-managed persistence, but I have read about it and there are examples in the book "seam in action", by Dan Allen. You use the @In annotion for seam managed persistence. Netbeans doesn't handle persistence or transactions either way, Glassfish and the persistence provider do that. Netbeans has a wizard for JPA that generates JPA entities from database tables, and queries for JSF pages or RESTful webservices. Netbeans does not have a wizard for seam, you can use the command line seam-gen for that, which you can also read about in the book seam in action

Hello, great examples, good commentary. I was wondering, in regard to your other blog article that you referenced at the top of this article, could you extend or change the example to include Seam-managed persistence in Netbeans? And if you don't mind, also Seam-managed transactions, also in Netbeans? Or perhaps it would be easier at this point in time to just do it all in Eclipse? And that begs the question, how easy can you do Seam-managed persistence and transactions in Eclipse, if at all?

Yes, I can :)))) Thank you for the hint :)

the restapi is added by netbeans 6. can you just remove this from the librairies? otherwise I will upload a version with this removed.

Bummer! But another question: if I use your example, I can't resolve the library problem: The project uses a class library called "restapi", but this class library was not found. What jars are bundled?

Je ne sais pas

I did it à la If you want to create your own Java EE application using Seam 2.0 on Glassfish V2 with Netbeans from scratch (read the steps in Brian Leonard's blog Seam Refresh but use the SEAM 2.0 jars listed here here): from the scrath with the help of netbeans6 and addes the necessary entries and the jars listed above :)

Did you use the jars listed in this blog? Brian's example needs the jar in this blog to work. I don't know, you could ask in the Glassfish forum.

Hi, I implemented the example of Brian Leonard with glassfish and Seam 2. But if I press the button, I get the exception javax.el.MethodNotFoundException: Method not found: de.barmenia.muv.seamNB6.service.ManagerActionLocal_9501578.sayHallo() I'm very suprised at the _9501578-extension. Have you any idea to fix my problem?

Yes I did implement a sample catalog app with Spring 2.5, see my previous blog entry. (but its not Seam and Spring together)

Hello! Did you try it with Spring 2.5? I get Validation Error (invalid stack size) when trying to use @Transactional and my aspect both under Spring 2.5. It seems that something wrong with CGLIB. I'm running my application with spring-agent and Spring JUnit integration annotation.

Venkatesh's example is interesting too, but what's special about Carol's is that she uses GlassFish's JPA implementation, not Hibernate. Nothing wrong with Hibernate, of course, but it is good to beat on TopLink to make sure it is up to snuff.

Hopefully, the next version of GlassFish will auto-discover the beans, just like JBoss does.

http://blogs.sun.com/venky/entry/developing_seam_application_with_woodstock Uses seam with woodstock on netbeans.