Skip navigation
Here is a screencast about a Comet application which allows users to watch and chat about a slide show. The Application loads the slide URLs from a RESTful Web Service and then uses dojo bayeux with Grizzly on Glassfish to publish the slides and/or text to all the slideshow subscribers.

You can read more about this application at RESTful Web Services and Comet

this is a write up of info I gathered from   Jay PipesHigh Performance MySQL , Colin Charles and others (see References below) for a Sun Tech Days MySQL presentation.

MySQL powers many high-volume Web sites,  including industry leaders such as Yahoo!, Alcatel-Lucent, Google, Nokia, YouTube, and Zappos.com.

MySQL for Developers

If you are a developer using MySQL, you should learn enough to take advantage of its strengths, because having an understanding of the database can help you develop better-performing applications. This can be especially important for Hibernate or JPA developers, since ORM frameworks tend to obscure the database schema and SQL  for the developer, which can lead to poorly-performing index and schema strategies and inefficient SQL.

MySQL Storage Engine Architecture

http://weblogs.java.net/blog/caroljmcdonald/archive/pluggaglearchitecture.gif
A key difference between MySQL and other database platforms is the pluggable storage engine architecture of MySQL, which allows you to select a specialized storage engine for a particular application need such as data warehousing, transaction processing, high availability...
A storage engine is  responsible for storing and retrieving all the data stored .  The storage engines have different functionality, capabilities and performance characteristics, in many applications choosing the right storage engine can greatly improve performance. Here is a brief summary of some of the more common engines: 
  • MyISAM - offers high-speed query and insert capability, is nontransactional, provides table-level locking, full-text indexing, compression, GIS functions, and supports indexes. MyISAM is a good choice for read-only or read-mostly applications that don't require transactions and issue primarily either SELECT or INSERT queries, such as Web Catalogs or listing of jobs, auctions, real estate, etc., or data warehousing applications. You can use multiple storage engines in a single application, sometimes specific components of an application (such as logging) fall into this category. MyISAM is not a good general purpose storage engine for high concurrent UPDATEs or DELETEs since these obtain exclusive write locks, however new rows can be inserted with shared read locks.
  • InnoDB - supports ACID transactions, multi-versioning, row-level locking, foreign key constraints, crash recovery,  and  good query performance depending on indexes. InnoDB uses row-level locking with multiversion concurrency control (MVCC). MVCC can allow fewer row locks by keeping data snapshots. Depending on the isolation level, InnoDB does not require any locking for a SELECT. This makes high concurrency possible, with some trade-offs: InnoDB requires more disk space compared to MyISAM, and for the best performance, lots of memory is required for the InnoDB buffer pool. InnoDB is a good choice for any order processing application, any application where transactions are required.  
  • Memory - stores all data in RAM for extremely fast access. Useful when you need fast access to data that doesn't change or doesn't need to persist after a restart.  Good for "lookup" or "mapping" tables, for caching the results of periodically aggregated data, for intermediate results when analyzing data.
    MEMORY tables do not support variable-length columns, this means that VARCHAR columns get implicitly converted to CHAR columns, and it is impossible to create a MEMORY table with TEXT or BLOB columns.
  • Merge - allows to logically group together a series of identical MyISAM tables and reference them as one object. Good for very large DBs like data warehousing.
  • Archive - provides for storing and retrieving large amounts of seldom-referenced historical, archived, or security audit information.
More specialized engines: 
  • Federated - Allows a remote server's tables to be used as if they were local, by linking together separate MySQL servers as one logical database. Can be good for distributed or data mart environments. 
  • Cluster/NDB-Highly-available clustered storage engine.  Should be used where very high speed, availability and redundancy are absolute requirements. Other tables (even in the same database) should use one or more of the other storage engines.
  • CSV-references comma-separated files as database tables.Useful for large bulk imports or exports
  • Blackhole- the /dev/null storage engine for temporarily disabling application input to the database .  Useful for benchmarking and some replication scenarios.
You can use multiple storage engines in a single application, a storage engine for the same table on a slave can be different than that of the master. This can be very useful for taking advantage of an engine's abilities.

Schema, the basic foundation of performance

Database normalization minimizes duplication of information, this makes updates simpler and faster because the same information doesn't have to be updated in multiple tables.  In a denormalized database, information is duplicated, or stored in multiple places. With a normalized database:
  • updates are usually faster.
  • there's less data to change.
  • tables are usually smaller, use less memory, which can give better performance.
  • better performance for distinct or group by queries
The disadvantages of a normalized schema are queries typically involve more tables and require more joins which can reduce performance.  Also normalizing may place columns in different tables that would benefit from belonging to the same index, which can also reduce query performance.  More normalized schemas are better for applications involving many transactions, less normalized are better for reporting types of application.  You should normalize your schema first, then de-normalize later.  Applications often need to mix the approaches, for example use a partially normalized schema, and duplicate, or cache, selected columns from one table in another table.

Data Types

In general, try to use the smallest data type that you can.  Small and simple data types usually give better performance because it  means fewer disk accesses (less I/O), more data in memory, and less CPU to process operations.

Numeric Data Types

MySQL has 9 numeric data types.

Whole Numbers:
 
TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT:   require 8, 16, 24, 32, and 64 bits of storage space, respectively. They can store values from –2(n-1) to 2(n-1)–1, where N is the number of bits of storage space they use. 

Use UNSIGNED when you don't need negative numbers, this doubles the bits of storage space.  BIGINT is not needed for AUTO_INCREMENT,  INT UNSIGNED stores 4.3 billion values!

INT(1) does not mean 1 digit!  The number in parentheses is the ZEROFILL argument, and specifies the number of characters some tools reserve for display purposes. For storage and computational purposes, INT(1) is identical to INT(20).

Integer data types work best for primary key data types. 

Real Numbers
:
FLOAT, DOUBLE: supports approximate calculations with standard floating-point math.
DECIMAL: use DECIMAL when you need exact results, always use for monetary/currency fields.

Other: 
BIT: to store  0,1 values. BIT(1) stores 1 bit, BIT(2) stores 2... max is 64.

Character Data Types

The CHAR and VARCHAR types are declared with a length that indicates the maximum number of characters to store.
VARCHAR(n)
stores variable-length character strings. VARCHAR uses only as much space as it needs, which helps performance because it saves disk space. Use when the maximum column length is larger than the average length and when updates to the field are rare, so fragmentation is not a problem.  VARCHAR gives more efficient disk usage, however MySQL internal memory buffers are fixed width, so  internal memory usage isn't more efficient, therefore try to keep VARCHAR Max Length short.
CHAR(n) is fixed-length: MySQL allocates enough space for the specified number of characters. Useful to store very short strings, when all the values are nearly the same length, and  for data that's changed frequently.

Use NOT NULL

Always define columns as NOT NULL unless there is a very good reason not to do so:
  • can save up to a byte per column per row of data
  • nullable columns make indexes, index statistics, and value comparisons more complicated.

Indexes

Indexes are data structures that help retrieve row data with specific column values faster. Indexes can especially improve performance for larger data bases.  Most MySQL storage engines support  B-tree indexes. a B-tree is (remember data strucutres 101 ?) a tree data structure that sorts data values, tree nodes define the upper and lower bounds of the values in the child nodes.  Leaf nodes have pointers to the data instead of pointers  to child nodes. B-trees are kept balanced by requiring that all leaf nodes are at the same depth.  MyISAM leaf nodes refer to the indexed row by the position of the row,  InnoDB leaf nodes refers to the index by its primary key values.

InnoDB's clustered indexesstore the row data in the leaf nodes, it's called clustered because rows with close primary key values are stored close to each other.  This can make retrieving indexed data fast, since the data is in the index.  But this can be slower for updates , secondary indexes, and for full table scans.

Covering Indexes are indexes that contain all the data values needed for a query, these queries can improve performance because the row does not have to be read.

Know Every SQL Statement

You need to understand the SQL queries your application makes and  evaluate their performance. For this you can harness theMySQL slow query log and use EXPLAIN.  Consider adding an index for queries that are taking too long, or cause a lot of load. Know how your query is executed by MySQL, consider disk I/O in particular.  Basically you want to make your queries access less data, Indexes are a good way to reduce data access.

Know Every SQL Statement for Hibernate/JPA developers

If you are using Hibernate, JPA, or another ORM framework you want to look at the generated SQL-statements. If you are using Hibernate, enable the Hibernate SQL log. If you are using Glassfish with Toplink as the JPA provider, you canlog the SQL that is being sent to the database by adding this property in the definition of the persistence unit in thepersistence.xml file, as follows: <property name="toplink.logging.level" value="FINE">. Then run a use case of your application and examine the SQL statements which are executed by JPA or your ORM framework. You want to make sure you are only retrieving the data your application needs, that is you want to optimize the number of SQL statements executed (see lazy loading below), and you want to examine the execution plan for queries.

Optimize data access

Basically you want to make your queries access less data: 
  • is your application retrieving more data than it needs, are queries accessing too many rows or columns?
  • is MySQL analyzing more rows than it needs?

Understanding EXPLAIN

EXPLAIN provides the execution plan chosen by the MySQL optimiser for a specific SELECT statement.  It is important to run EXPLAIN on all SELECT statements that your code is executing against the database.  This step ensures that missing indexes are picked up early in the development process and gives developers insight into how the MySQL optimizer has chosen to execute the query.

To use it just precede a SELECTstatement with the keyword EXPLAINand  MySQL will display information from the optimizer about the query execution plan, which explains how it would process theSELECT,  including information about how tables are joined and in which order. The EXPLAIN EXTENDED SELECT variant, followed by SHOW WARNINGS gives some additional information, including the final rewritten query as used by the optimizer.
http://weblogs.java.net/blog/caroljmcdonald/archive/explain.gif
With the help of EXPLAIN, you can see where you should add indexes to tables to get a faster SELECT that uses indexes to find rows. You can also use EXPLAIN to check whether the optimizer joins the tables in an optimal order.

EXPLAIN
returns a row of information for each "table" used in the SELECT statement, which shows each part and the order of the execution plan.  The "table" can mean a real schema table, a derived or temporary table, a subquery, a union result. Here is an explanation of some of the output for each row: 
  • table:  the real table name or an alias.
  • select_type: shows whether the row is a simple or complex SELECT. If complex itcan be: PRIMARY (Outermost SELECT), UNION, SUBQUERY, DERIVED, or UNCACHEABLE.
  • type: shows the "access strategy" type to find rows. The different access types ordered from the best  to worst: 
    • system, or const: very fast because  the table has at most one matching row (For example a primary key used in the WHERE)
    • eq_ref: index lookup returning one value
    • ref: index access
    • fulltext:  FULLTEXT index.
    • ref_or_null:  like ref, but with an extra search for rows that contain NULL values.
    • range:   index scan to select the rows in given range
    • index:   index tree is scanned.
    • ALL: A full table scan
  • key: the index that MySQL decided to use, from the possible_keys, to look up rows.
  • rows: the number of rows MySQL estimates it must examine to execute the query.
  • ref: columns or constants which are compared to key to select rows .
  • Extra: additional information about how MySQL resolves the query. Watch out for Extra values of Using filesort and Using temporaryUsing index means information is retrieved from the table using only information in the index tree without having to do an additional seek to read the actual row. This strategy can be used when the query uses only columns that are part of a single index (Covering Index).
When do you get a full table scan? 
  • No WHERE condition
  • No index on any field in WHERE condition
  • When your range returns a large number of rows, i.e. too many records in WHERE condition
  • When optimizer sees that the condition will return > ~20% of the records in a table, it will prefer a scan versus many seeks
  • Pre-MySQL 5, using OR in a WHERE clause 
    • now fixed with an index merge, so the optimiser can use more than one index to satisfy a join condition
How do you know if a scan is used?
In the EXPLAIN output, the “type” for the table/set will be “ALL” or “index”.  “ALL” means a full table data record scan is performed.  “index” means a full index record scan.  Avoid this by ensuring indexes are on columns that are used in the WHERE, ON, and GROUP BY clauses.

Scans and seeks

A seek jumps into a random place (on disk or in memory) to fetch data. A scan will jump to the start of the data, and sequentially read (from either disk or memory) until the end of the data. With large amounts of data, sequentially scanning through contiguous data on disk or in memory is faster than performing many random seek operations.  But scans can also be a sign of poor indexing, if an index contains many rows with the same value it is not very useful.  MySQL keeps stats about the uniqueness of values in an index in order to estimate the rows returned (rows in the explain output). If the rows is greater than a certain  % of rows in the table, then MySQL will do a scan.

Indexed columns and functions don't mix

Indexes can quickly find the rows that match a WHERE clause, however this works only if the index is NOT used in a function or expression in the WHERE clause. Below is an example where an index is used :

mysql> EXPLAIN SELECT * FROM film WHERE title LIKE 'Tr%'\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: film
         type: range
possible_keys: idx_title
          key: idx_title
      key_len: 767
          ref: NULL
         rows: 15
        Extra: Using where


In the example above, a  fast range "access strategy" is chosen by the optimizer, and the index scan on title is used to winnow the query results down.
Below is an example where an index can not be used :

mysql> EXPLAIN SELECT * FROM film WHERE LEFT(title,2) = 'Tr' \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: film
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 951
        Extra: Using where


A slow full table scan (the ALL"access strategy") is used because a function (LEFT) is operating on the title column. Operating on an indexed column with a function (in this case the LEFT() function) means the optimizer cannot use the index to satisfy the query.  Typically, you can rewrite queries in order to not operate on an indexed column with a function.

Solving multiple problems in one query

Here is an example of improving a query:

SELECT * FROM Orders WHERE TO_DAYS(CURRENT_DATE()) – TO_DAYS(order_created) <= 7;

First, we are operating on an indexed column (order_created) with a function TO_DAYS– let's fix that:

SELECT * FROM Orders WHERE order_created >= CURRENT_DATE() - INTERVAL 7 DAY;

Although we rewrote the WHERE expression to remove the function on the index, we still have a non-deterministic functionCURRENT_DATE() in the statement, which eliminates this query from being placed in the query cache. Any time a non-deterministic function is used in a SELECT statement, the query cache ignores the query.  In read-intensive applications, this can be a significant performance problem. – let's fix that:

SELECT * FROM Orders WHERE order_created >= '2008-01-11' - INTERVAL 7 DAY;

We replaced the function with a constant (probably using our application programming language).  However, we are specifying SELECT * instead of the actual fields we need from the table.  What if there is a TEXT field in Orders called order_memo that we don't need to see?  Well, having it included in the result means a larger result set which may not fit into the query cache and may force a disk-based temporary table. – let's fix that:

SELECT order_id, customer_id, order_total, order_created
FROM Orders WHERE order_created >= '2008-01-11' - INTERVAL 7 DAY;


A Few Things to consider for  JPA/Hibernate  devlopers:

Lazy loading and JPA

With JPA many-to-one and many-to-many relationships lazy load by default , meaning they will be loaded when the entity in the relationship is accessed. Lazy loading is usually good, but if you need to access all of the "many" objects in a relationship, it will cause n+1 selects where n is the number of  "many" objects.  You can change the relationship to be loaded eagerly as follows :
 
public class Employee{

    @OneToMany(mappedBy = "employee", fetch = FetchType.EAGER)
    private Collection<Address> addresses;

.....
}
However you should be careful with eager loading which could cause SELECT statements that fetch too much data. It can cause a Cartesian product  if you eagerly load entities with several related collections. 

If you want to temporarily override the LAZY fetch type, you could use Fetch Join.  For example this query would eagerly load the employee addresses:  

@NamedQueries({ @NamedQuery(name="getItEarly",
                 query="SELECT e FROM Employee e JOIN FETCH e.addresses")})

public class Employee{
.....
}

Optimistic locking and JPA

The Java Persistence API 1.0 persistence providers support optimistic locking, where your application will get an exception whenever a transaction tries to commit an object that was updated in the database since this transaction began. To enable this for a entity, you need to add a version attribute:

@Entity
public class Item {
    ...
    @Version
    private int version;
    ...
}


In Conclusion

  • Understand the storage engines
  • Keep data types small and compact
  • Understand your query execution plans with the EXPLAIN output
  • Understand the scan vs. seek choice the optimizer must make
  • Don't mix functions and indexed columns

References

High Performance MySQL book
MySQL Pluggable Storage Engine Architecture
MySQL Storage Engine Architecture, Part 2: An In-Depth Look
Optimizing Queries with EXPLAIN
Java Persistence with Hibernate book
Jay Pipes blog 
Colin Charles blog
mysql performance blog
Ronald Bradford blog
Taking JPA for a Test Drive
Pro EJB 3: Java Persistence API
Pro MySQL, Chapter 6: Benchmarking and Profiling




Rick Palkovic and I have written an article about a Comet application which allows users to watch and chat about a slide show. The Application loads the slide URLs from a RESTful Web Service and then uses dojo bayeux with Grizzly on Glassfish to publish the slides and/or text to all the slideshow subscribers. See RESTful Web Services and Comet. RESTful Web Services and Comet

a RESTful Pet Catalog


This Sample Pet Store Catalog application shows how to expose a  Catalog  as a RESTful Web Service for remote client applications, and it shows how to code a Dojo client which  gets and displays the Web Service responses in a dynamic Ajax table ( Dojo grid). I re-implemented this Sample Catalog application implemented with JAX-WS on the server side and JSF on the client side which is also available in theJava One Metro hands on lab.

Download the RESTful Pet Catalog Code

Dojo  is an open source DHTML toolkit written in JavaScript.

JAX-RS provides a standardized API for building RESTful web services in Java. Central to the RESTful architecture is the concept of resources identified by universal resource identifiers (URIs). The API  provides a set of annotations which you can add to Plain Old Java Objects (POJOs)  to expose web resources identified by URIs .

Explanation of the usage of Dojo and JAX-RS in a sample Catalog Application

The image below shows the Catalog Listing page, which allows a user to page through a list of items in a store.
petcatalog

Quick installation and use of dojo with Netbeans

There are 3 ways to install dojo which you can read about in the book of dojo. A quick and easy way to use dojo with Netbeans is to download the JavaScript libraries from http://dojotoolkit.org/downloads.   Create a new NetBeans Web Applications project. Extract the dojo toolkit  into the project web directory: .../web , then rename dojo-release-1.1.1/ to src/  this will give you the project structure shown below.  I have already done this for the sample project so you do not have to download dojo in order to run the sample.
    dojonetproj.JPG

Dojo style sheets

Every page using the dojo Grid needs to import the grid style sheetGrid.css as shown below:

        
Code Sample from:  index.html

    <style type="text/css">
      /* tundraGrid.css matches Dijit Tundra style. */
      @import "src/dojox/grid/_grid/tundraGrid.css";
      @import "src/dijit/themes/tundra/tundra.css";
      @import "src/dojo/resources/dojo.css";
      @import "src/dojox/grid/_grid/Grid.css";
    </style>



This will load the the CSS files required by the Dojo grid widget, you can just use  dojox/grid/_grid/Grid.cssinstead of tundraGrid if you don't want the tundra style.  

Loading base dojo and required modules into an application

In order to load dojo into your application,  put the relative path to the dojo.js file in a script element in the head section of your  HTML page as shown below:

        
Code Sample from:  index.html

 <script type="text/javascript" src="src/dojo/dojo.js"
           djConfig="isDebug: true, debugAtAllCosts: false,
           parseOnLoad: true">
 </script>




This script element will load the base dojo script which gives you access to all the dojo functionality.

Next  the application specifies which  dojo modules to load, using  the dojo.require function (kind of like  import in Java):

        
Code Sample from:  index.html

 <script type="text/javascript">
   dojo.require("dojox.grid.Grid");
   dojo.require("dojox.grid._data.model");
   dojo.require("dojo.parser");
 </script>


Dojo is organized into three major layers: Dojo Core, Dijit, and DojoX.   DojoX builds on  Dojo Core and provides newer extensions to the Dojo toolkit. The rest of the Java Script for this application is in the file dynamicTable.js.

The Grid Widget

You can use widgets declaratively by using special attributes inside of regular HTML tags, or programmatically through JavaScript.
The dojoType attribute declares a Dojo widget. Below is the declaration of the Grid widget for this applicaton:
            
Code Sample from:  index.html

<div id="grid" dojoType="dojox.Grid" model="model" structure="layout">
</div>


The model and structure attributes point to the  JavaScript variables for the model and layout structure explained below.

The Grid View

A Dojo grid  is a widget useful for displaying data sets in a table with its own scrollable views.  The dojo grid widget requires a layout. A grid layout is declared as an array of views.  Each view is a group of columns,  declared as an array of arrays. Each array element is an object, the "name" property of the object names the column. The column names will be displayed in the top row of the grid. The code below declares 4 columns: Company,City, State, Zip. This grid layout structure consists of one view as shown  below:

        
Code Sample from:  dynamicTable.js

formatImage = function(value) {
    if (!value)
        return ' ';   
        return "<img src='" + value + "'/>";   
};

 
// Data Grid layout
// A grid view is a group of columns
var view1 = {
            cells: [
        [
            {name: 'Name', field: "name"},
            {name: 'Description', field: "description", width: '200px'},
            {name: 'Photo',field: "imagethumburl", formatter: formatImage, width: '120px'},
            {name: 'Price',field: "price"}
        ]
    ]
};
// a grid layout is an array of views.
var layout = [ view1 ];




The Grid Model

The dojo grid widget requires a data model. The model variable declares the type of Dojo object that the Grid will use for the json data that will be loaded in the grid. There are different options for the model, this example uses thedojox.grid.data.Objects which is a collection of objects to be displayed in the grid.

        
Code Sample from:  dynamicTable.js

// the model will contain the data to be displayed in the view
model = new dojox.grid.data.Objects(null,null);

function handleResponse(responseObject, ioArgs){
    // set the model object with the returned items list
    model.setData(responseObject.items.item);       
}  

// make request to the items web service
function loadTable(page){
    start = page * batchSize;
    var targetURL = "resources/items/?start="+ encodeURIComponent(start);   
    dojo.xhrGet({
        url: targetURL,
        handleAs: "json",
        load: handleResponse,
        error: handleError
    });
}

The loadTablefunction calls   dojo.xhrGetto make an XMLHttpRequest to the items JAX-RS web service specified by the url: parameter. When the response from web service is returned, the callback function handleResponse specified by load: is called and the response is passed to the callback function in the responseObject. The handleAs  parameter specifies the response data type, handleAs: "json"  means the returned data is of the type JSON (Java Script object notation).
In the   handleResponse callback function,  model.setData is called to populate the Dojo grid  with the data returned from the  the items JAX-RS web service. Below is an example of a JSON response from the items JAX-RS web service:

        
Example json data

{"items":
  {"@uri":"http://host/catalog/resources/items/",
   "item":[
     {"@uri":"http://host/catalog/resources/items/1/",
       "name":"Friendly Cat",
      "description":"This black and white colored cat is super friendly.",     
       "id":"1",
       "imageurl":"http://localhost:8080/CatalogService/images/anthony.jpg"},
     {"@uri":"http://host/catalog/resources/items/2/",
       "name":"Fluffy Cat",
       "description":"A great pet for a hair stylist!
       "id":"2",
       "imageurl":"http://localhost:8080/CatalogService/images/bailey.jpg"}
    ]
  }
}


Loading the table

The dojo.addOnLoad function allows you to call a  function after a page has loaded and after Dojo has finished its initilization. This application uses dojo.addOnLoad to call the loadTable() function (which we looked at above)  which calls the  items JAX-RS web service and sets the results in the grid data model.

        
Code Sample from:  dynamicTable.js

    <script type="text/javascript">
        dojo.addOnLoad(function(){
            loadTable(0);
        });
    </script>

 

Events for paging

The  "<<"">>"buttons call the next() previous()functions when clicked:

        
Code Sample from:  index.html

<input type="button" value="<<" onclick="previous();">
</input>
<input type="button" value=">>" onclick="next();">
</input>


The next()function  increments the page number and then calls theloadTable() funtion:

        
Code Sample from:dynamicTable.js


function next() {
    page =page + 1;
    loadTable(page);
}

function previous() {
    page =page - 1;
    if (page < 0) page = 0;
    loadTable(page);
}


RESTful  Web Services with JAX-RS

The  dojo.xhrGet url: parameter  references the URI resources/items/for the items   RESTful web service.  The items RESTful web service was generated using Netbeans 6.1 as explained in the Generating RESTful Web Services from Entity Classes  tutorial.  Using Netbeans 6.1 you can generate JPA Entity Classes from Database tables, then you can Generate RESTful Web Services from Entity Classes, and then you can test the Web Services with a browser interface. The items RESTful web service was generated from the item data base table (the sql is in the how to run section). 

Below is a snippet from the ItemsResource.java class which was generated by the Netbeans "Generate RESTful Web Services from Entity Classes" feature :

        
Code Sample from: ItemsResource.java

// Service URI path "/items/"

@Path("/items/")

public class 
ItemsResource {
  
    @GET
    @ProduceMime("application/json")
    public
ItemsConverterget(@QueryParam("start")
           @DefaultValue("0") int start, @QueryParam("max")
            @DefaultValue("4") int max, @QueryParam("expandLevel")
            @DefaultValue("1") int expandLevel, @QueryParam("query")
            @DefaultValue("SELECT e FROM Item e") String query) {
        try {
            ItemsConverter items = new ItemsConverter(
               getEntities(start, max, query),
                context.getAbsolutePath(), expandLevel);
            return
items;
        } finally {
            PersistenceService.getInstance().close();
        }
    }


The ItemsResourcerepresents a list of items. The ItemsResource getmethod returns a list of Item objects in JSON format. 
  • To address a resource in REST you specify its URI. @Path is a JAX-RS annotation that identifies the URI path for the resource. For the ItemsResource  the URI path is /items/.
  • @GET specifies that thegetmethod supports the HTTP GET method.
  • @ProduceMimespecifies the MIME types that a method can produce. Here, the annotation specifies that the getmethod returns a JSONArray object.  TheItemsConverterclass is a JAXB annotated class which is used to marshal a list of Item objects into XML orJSON format.   The getEntitiesmethod returns a list of Item entity objects and is explained below.  
  • @QueryParamspecifies input parameters for methods.  When the method is invoked, the input value will be injected into the corresponding input argument. 
  • @DefaultValuespecifies a default value for an arguement if no input value is given.
Here is an example of an HTTP request for this Web Service:

    
Request: GET http://host/catalog/resources/items/?start=0


Here is an example of an HTTP response for this Web Service:

    
Received:
{"items":
  {"@uri":"http://host/catalog/resources/items/",
   "item":[
     {"@uri":"http://host/catalog/resources/items/1/",
       "name":"Friendly Cat",
      "description":"This black and white colored cat is super friendly.",     
       "id":"1",
       "imageurl":"http://localhost:8080/CatalogService/images/anthony.jpg"},
     {"@uri":"http://host/catalog/resources/items/2/",
       "name":"Fluffy Cat",
       "description":"A great pet for a hair stylist!
       "id":"2",
       "imageurl":"http://localhost:8080/CatalogService/images/bailey.jpg"}
    ]
  }
}


The ItemsConverterclass is a JAXB annotated class, used to marshal a list of Item objects into XML or  JSON format.  A snippet of the ItemsConverterclass is shown below:


        
Code Sample from: ItemsConverter.java

@XmlRootElement
public class ItemsConverter {

    @XmlElement
    public Collection<ItemConverter> getItem(){
       ...
       return items;
    }
    @XmlAttribute
    public URI getUri() {
        return uri;
    }



Java Persistence Query API

The ItemsResourcegetEntities method uses the Java Persistence API Queryobject to return a list of items.

        
Code Sample from: ItemsResource.java

@Path("/items/")

public class 
ItemsResource {
  
    . . .

    protected Collection<Item> getEntities(int start, int max, String query) {
       
PersistenceService ps =PersistenceService.getInstance();
        Query query = ps.
createQuery(query);
        query.
setFirstResult(start);
       query.setMaxResults(max);
        returnquery.getResultList();
    }

 

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: query.setMaxResults(int maxResult) sets the maximum number of results to retrieve.query.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:
  1. annotating the class with an @Entityannotation.
  2. 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. 
For more information on Netbeans and JPA see basics of developing a web application using Java™ Persistence API.


        
Code Sample from: Item.java

@Entity

public class Itemimplements Serializable {

   
@Id 
    private Long id;

    private String name;
    private String description; 
    private String imageurl; 
    private String state; 
    private BigDecimal price;

    
    public
Item() { }
     
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

   . . .

}   




Conclusion
This concludes the sample application which  demonstrates a RESTful Web Service, coded using JAX-RS: Java API for RESTful Web Services (JSR-311) , which provides a list of items, and a dojo client which  gets and displays the Web Service responses in a dynamic Ajax table.

Configuration of the Application for Netbeans 6.5m1 , Glassfish V2, and MySQL


Running the Sample Code

  1. Download the sample code and extract its contents. You should now see the newly extracted directory as<sample_install_dir>/catalog, where<sample_install_dir> is the directory where you unzipped the sample package. For example, if you extracted the contents to C:\ on a Windows machine, then your newly created directory should be at C:\catalog.
  2. Start NetBeans IDE. Click Open Project in the File menu and select the catalog directory you just unzipped.
  3. Start the MySQL database as follows: 
    • Click the Services tab in the NetBeans IDE.
    • Expand the databases node. You should see the MySQL server database in the list of databases.       
      Database list
    • Right-mouse click on the MySQL server database and select Start.
  4. Create the catalog database as follows: 
    • Right-mouse click on the MySQL server database and select Create Database.
    • Enter the database name catalog. This will open a New Database Connection window. Click O.K. to accept the displayed settings.
  5. Create the tables in the MySQL catalog database as follows: 
    • Expand the Drivers node. You should a driver for the catalog database in the list of drivers.       
      Driver list
    • Right-mouse click on the catalog driver and select Connect.
    • Right-mouse click on the catalog driver and select Execute Command. This will open up a SQL command window.
    • Copy the contents of the createdbmysql.sql file in the catalog directory and paste the contents into the SQL command window.
    • Click the Run SQL icon Run SQL icon(Ctrl+Shift+E) above the SQL command window.
  6. Build the project as follows:

     
    • Right click the catalog node in the Projects window.
    • Select Clean and Build Project.

  7. Run the project as follows:

     
    • Right click the catalog node in the Projects window.
    • Select Run Project.
When you run the project, your browser should display the opening page of the Sample Application (at http://localhost:8080/catalog/).

For more Information:




a Dynamic Ajax table example using dojo and RESTful Web Services on Glassfish


This Sample Catalog app demonstrates a RESTful Web Service, coded using JAX-RS: Java API for RESTful Web Services (JSR-311)and Java Persistence API, which provides a list of customers, and a Dojo client which  gets and displays the Web Service responses in a dynamic Ajax table ( Dojo grid).

Download the dojo Sample Application Code

Dojo  is an open source DHTML toolkit written in JavaScript.

JAX-RS provides a standardized API for building RESTful web services in Java. Central to the RESTful architecture is the concept of resources identified by universal resource identifiers (URIs). The API  provides a set of annotations which you can add to Plain Old Java Objects (POJOs)  to expose web resources identified by URIs .

Explanation of the usage of Dojo and JAX-RS in a sample Catalog Application

The image below shows the Customer Listing page, which allows the user to page through a list of customers.
    
    table.jpg


Quick installation and use of dojo with Netbeans

There are 3 ways to install dojo which you can read about in the book of dojo. A quick and easy way to use dojo with Netbeans is to download the JavaScript libraries from http://dojotoolkit.org/downloads.   Create a new NetBeans Web Applications project. Extract the dojo toolkit  into the project web directory: .../web , then rename dojo-release-1.1.1/ to src/  this will give you the project structure shown below.  I have already done this for the sample project so you do not have to download dojo in order to run the sample.
    dojonetproj.JPG

Dojo style sheets

Every page using the dojo Grid needs to import the grid style sheetGrid.css as shown below:

        
Code Sample from:  index.html

    <style type="text/css">
      /* tundraGrid.css matches Dijit Tundra style. */
      @import "src/dojox/grid/_grid/tundraGrid.css";
      @import "src/dijit/themes/tundra/tundra.css";
      @import "src/dojo/resources/dojo.css";
      @import "src/dojox/grid/_grid/Grid.css";
    </style>



This will load the the CSS files required by the Dojo grid widget, you can just use  dojox/grid/_grid/Grid.cssinstead of tundraGrid if you don't want the tundra style.  

Loading base dojo and required modules into an application

In order to load dojo into your application,  put the relative path to the dojo.js file in a script element in the head section of your  HTML page as shown below:

        
Code Sample from:  index.html

 <script type="text/javascript" src="src/dojo/dojo.js"
           djConfig="isDebug: true, debugAtAllCosts: false,
           parseOnLoad: true">
 </script>




This script element will load the base dojo script which gives you access to all the dojo functionality.

Next  the application specifies which  dojo modules to load, using  the dojo.require function (kind of like  import in Java):

        
Code Sample from:  index.html

 <script type="text/javascript">
   dojo.require("dojox.grid.Grid");
   dojo.require("dojox.grid._data.model");
   dojo.require("dojo.parser");
 </script>


Dojo is organized into three major layers: Dojo Core, Dijit, and DojoX.   DojoX builds on  Dojo Core and provides newer extensions to the Dojo toolkit. The rest of the Java Script for this application is in the file dynamicTable.js.

The Grid Widget

You can use widgets declaratively by using special attributes inside of regular HTML tags, or programmatically through JavaScript.
The dojoType attribute declares a Dojo widget. Below is the declaration of the Grid widget for this applicaton:
            
Code Sample from:  index.html

<div id="grid" dojoType="dojox.Grid" model="model" structure="layout">
</div>


The model and structure attributes point to the  JavaScript variables for the model and layout structure explained below.

The Grid View

A Dojo grid  is a widget useful for displaying data sets in a table with its own scrollable views.  The dojo grid widget requires a layout. A grid layout is declared as an array of views.  Each view is a group of columns,  declared as an array of arrays. Each array element is an object, the "name" property of the object names the column. The column names will be displayed in the top row of the grid. The code below declares 4 columns: Company,City, State, Zip. This grid layout structure consists of one view as shown  below:

        
Code Sample from:  dynamicTable.js

 
// Data Grid layout
// A grid view is a group of columns
var view1 = {
            cells: [
                [
                    {name: 'Company', field: "name"},
                    {name: 'City', field: "city"},
                    {name: 'State',field: "state"},
                    {name: 'Zip',field: "zip"}
                ]
            ]
};
// a grid layout is an array of views.
var layout = [ view1 ];


This grid layout for this example is shown in the figure below (note: how the data for the table gets loaded is explained below).
    dojogrid.JPG

The Grid Model

The dojo grid widget requires a data model. The model variable declares the type of Dojo object that the Grid will use for the json data that will be loaded in the grid. There are different options for the model, this example uses thedojox.grid.data.Objects which is a collection of objects to be displayed in the grid.

        
Code Sample from:  dynamicTable.js

// the model will contain the data to be displayed in the view
model = new dojox.grid.data.Objects(null,null);

function handleResponse(responseObject, ioArgs){
    // set the model object with the returned customers list
    model.setData(responseObject.customers.customer);       
}  

// make request to the customers web service
function loadTable(page){
    start = page * batchSize;
    var targetURL = "resources/customers/?start="+ encodeURIComponent(start);   
    dojo.xhrGet({
        url: targetURL,
        handleAs: "json",
        load: handleResponse,
        error: handleError
    });
}

The loadTablefunction calls   dojo.xhrGetto make an XMLHttpRequest to the customers JAX-RS web service specified by the url: parameter. When the response from web service is returned, the callback function handleResponse specified by load: is called and the response is passed to the callback function in the responseObject. The handleAs  parameter specifies the response data type, handleAs: "json"  means the returned data is of the type JSON (Java Script object notation).
In the   handleResponse callback function,  model.setData is called to populate the Dojo grid  with the data returned from the  the customers JAX-RS web service. Below is an example of a JSON response from the customers JAX-RS web service:

        
Example json data

{"customers":
  {"@uri":"http://host/dojoRest/resources/customers/",
   "customer":[
     {"@uri":"http://host/dojoRest/resources/customers/1/",
       "name":"JumboCom",
      "city":"Fort Lauderdale",     
       "state":"FL",
       "zip":"33015"},
     {"@uri":"http://host/dojoRest/resources/customers/2/",
       "name":"Livermore Enterprises",
       "city":"Miami",
       "state":"FL",
       "zip":"33055"}
    ]
  }
}


Loading the table

The dojo.addOnLoad function allows you to call a  function after a page has loaded and after Dojo has finished its initilization. This application uses dojo.addOnLoad to call the loadTable() function (which we looked at above)  which calls the  customers JAX-RS web service and sets the results in the grid data model.

        
Code Sample from:  dynamicTable.js

    <script type="text/javascript">
        dojo.addOnLoad(function(){
            loadTable(0);
        });
    </script>

 

Events for paging

The  "<<"">>"buttons call the next() previous()functions when clicked:

        
Code Sample from:  index.html

<input type="button" value="<<" onclick="previous();">
</input>
<input type="button" value=">>" onclick="next();">
</input>


The next()function  increments the page number and then calls theloadTable() funtion:

        
Code Sample from:dynamicTable.js


function next() {
    page =page + 1;
    loadTable(page);
}

function previous() {
    page =page - 1;
    if (page < 0) page = 0;
    loadTable(page);
}


RESTful  Web Services with JAX-RS


The  dojo.xhrGet url: parameter  references the URI resources/customers/for the customers   RESTful web service.  The customers RESTful web service was generated using Netbeans 6.1 as explained in the Generating RESTful Web Services from Entity Classes  tutorial.  Using Netbeans 6.1 you can generate JPA Entity Classes from Database tables, then you can Generate RESTful Web Services from Entity Classes, and then you can test the Web Services with a browser interface. The customers RESTful web service was generated from the customer data table which comes already created in the Java DB with Netbeans. 

Below is a snippet from the CustomersResource.java class which was generated by the Netbeans "Generate RESTful Web Services from Entity Classes" feature :

        
Code Sample from: CustomersResource.java

// Service URI path "/customers/"

@Path("/customers/")

public class CustomersResource{
  
    @GET
    @ProduceMime("application/json")
    public
CustomersConverterget(@QueryParam("start")
           @DefaultValue("0") int start, @QueryParam("max")
            @DefaultValue("4") int max, @QueryParam("expandLevel")
            @DefaultValue("1") int expandLevel, @QueryParam("query")
            @DefaultValue("SELECT e FROM Customer e") String query) {
        try {
            CustomersConverter custs = new CustomersConverter(
               getEntities(start, max, query),
                context.getAbsolutePath(), expandLevel);
            return
custs;
        } finally {
            PersistenceService.getInstance().close();
        }
    }


The CustomersResourcerepresents a list of customers. The CustomersResource getmethod returns a list of Customer objects in JSON format. 
  • To address a resource in REST you specify its URI. @Path is a JAX-RS annotation that identifies the URI path for the resource. For the CustomersResource  the URI path is /customers/.
  • @GET specifies that thegetmethod supports the HTTP GET method.
  • @ProduceMimespecifies the MIME types that a method can produce. Here, the annotation specifies that the getmethod returns a JSONArray object.  TheCustomersConverterclass is a JAXB annotated class which is used to marshal a list of Customer objects into XML or JSON format.   The getEntitiesmethod returns a list of Customer entity objects and is explained below.  
  • @QueryParamspecifies input parameters for methods.  When the method is invoked, the input value will be injected into the corresponding input argument. 
  • @DefaultValuespecifies a default value for an arguement if no input value is given.
Here is an example of an HTTP request for this Web Service:

    
Request: GET http://host/dojoRest/resources/customers/?start=0


Here is an example of an HTTP response for this Web Service:

    
Received:
{"customers":
  {"@uri":"http://host/dojoRest/resources/customers/",
   "customer":[
     {"@uri":"http://host/dojoRest/resources/customers/1/",
       "name":"JumboCom",
      "city":"Fort Lauderdale",     
       "state":"FL",
       "zip":"33015"},
     {"@uri":"http://host/dojoRest/resources/customers/2/",
       "name":"Livermore Enterprises",
       "city":"Miami",
       "state":"FL",
       "zip":"33055"}
    ]
  }
}


The CustomersConverterclass is a JAXB annotated class, used to marshal a list of Customer objects into XML or  JSON format.  A snippet of the CustomersConverterclass is shown below:


        
Code Sample from: CustomersConverter.java

@XmlRootElement
public class CustomersConverter {

    @XmlElement
    public Collection<CustomerConverter> getCustomer(){
       ...
       return items;
    }
    @XmlAttribute
    public URI getUri() {
        return uri;
    }



Java Persistence Query API

The CustomersResourcegetEntities method uses the Java Persistence API Queryobject to return a list of customers.

        
Code Sample from: CustomersResource.java

@Path("/customers/")

public class CustomersResource{
  
    . . .

    protected Collection<Customer> getEntities(int start, int max, String query) {
       
PersistenceService ps =PersistenceService.getInstance();
        Query query = ps.
createQuery(query);
        query.
setFirstResult(start);
       query.setMaxResults(max);
        returnquery.getResultList();
    }

 

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: query.setMaxResults(int maxResult) sets the maximum number of results to retrieve.query.setFirstResult(int startPosition) sets the position of the first result to retrieve.

In the code below, we show the Customerentity class which maps to the  CUSTOMER table that stores the customer instances. This is a typical Java Persistence entity object. There are two requirements for an entity:
  1. annotating the class with an @Entityannotation.
  2. 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. 
For more information on Netbeans and JPA see basics of developing a web application using Java™ Persistence API.


        
Code Sample from: Customer.java

@Entity

public class Customerimplements Serializable {

   
@Id 
    private Integer customerId;

    private String name;
    private String addressline1;   
    private String city;  
    private String state; 
    private String zip;

    
    public
Customer() { }
     
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }



}   




Conclusion
This concludes the sample application which  demonstrates a RESTful Web Service, coded using JAX-RS: Java API for RESTful Web Services (JSR-311) , which provides a list of customers, and a dojo client which  gets and displays the Web Service responses in a dynamic Ajax table.

Configuration of the Application for jMaki, JPA, Netbeans 6.1 and Glassfish V2

  • Download and install NetBeans 6.1 bundled with GlassFish V2
  • Alternatively you can  Downloadand install GlassFish V2 separately.

Open and Run the Sample code:

  1. Download the sample code and extract its contents. You should now see the newly extracted directory as<sample_install_dir>/dojoRest, where <sample_install_dir> is the directory where you installed the sample package. For example, if you extracted the contents to C:\ on a Windows machine, then your newly created directory should be atC:\dojoRest.

  2. Start the NetBeans IDE. Click Open Project in the File menu and select the dojoRest directory you just unzipped.

  3. Build the project as follows:

     
    • Right click the dojoRest node in the Projects window.
    • Select Clean and Build Project.

  4. Run the project as follows:

     
    • Right click the dojoRest node in the Projects window.
    • Select Run Project.
When you run the project, your browser should display the opening page of the Sample Application (at http://localhost:8080/dojoRest/).

References:




a Dynamic Ajax table example using jMaki and RESTful Web Services on Glassfish


This Sample Catalog app demonstrates a RESTful Web Service, coded using JAX-RS: Java API for RESTful Web Services (JSR-311)and Java Persistence API, which provides a list of customers, and a jMaki client which  gets and displays the Web Service responses in a dynamic Ajax table.

Download the jMaki Sample Application Code

jMaki is an Ajax framework that provides a lightweight model for creating JavaScript centric Ajax-enabled web applications. jMaki provides wrappedwidgets that can be used as JavaServer Pages tags, as JavaServer Faces components, within a Phobos application, or with PHP. This sample applicaton uses jMaki with JavaServer Pages.

JAX-RS provides a standardized API for building RESTful web services in Java. Central to the RESTful architecture is the concept of resources identified by universal resource identifiers (URIs). The API  provides a set of annotations which you can add to Plain Old Java Objects (POJOs)  to expose web resources identified by URIs .

Explanation of the usage of jMaki and JAX-RS in a sample Catalog Application

The image below shows the Customer Listing page, which allows the user to page through a list of customers.

pagingtable.jpg

jMaki dataTable widget

With  jMaki and JavaServer Pages, you can easily include wrapped widgets from ajax toolkits into a JavaServer Page as a custom JSP tag. With the Netbeans jMaki plugin you can drag  jMaki widgets from the Palette into a JSP. jMaki standardizes widget data and event models to simplify the programming model and to simplify interactions between widgets.

The sample application's index.jsp page uses a jMaki yahoo.dataTable widget to display a  list of customers in a dynamic table.

The jMaki table widgets (there is also a jMaki dojo table widget) are useful when you want to show a set of results in tabular data on a web page.  Table widgets provide sortable columns, row selection, and they can be updated using jMaki publish subscribe events.

In the List.jsp web page the dataTable is defined as shown below:   (Note:Redcolors are for jMaki tags or variables,  and Green for my code orvariables)

        
Code Sample from:  index.jsp

<a:widget name="yahoo.dataTable"
          subscribe="/table"
          service="webresources/customers/jMakiTable"/>


To determine the data format and events for the table you can refer to the  jMaki Table Data Model or look at the widget.json file for the table widget. This file is located in the resources/yahoo/dataTable directory.

The serviceattribute references the customers/jMakiTableRESTful web service which returns the data to be included in the table.  The data for the table should be a JSON object containing an object of columns and an array of row arrays. The column names need a unique id which is then used in the data to associate it with a given row. An example for a table of companies is shown below:


        
Code Sample from:  widget.json
{
 'columns':[
     {'label' :'Company', 'id' : 'name'},
     {'label':'City', 'id' : 'city'},
     {'label':'City', 'id' : 'state'}
 ],
 'rows':[
     {'name' : 'Sun Microsystems', 'city' : 'Santa Clara',
'state' : 'CA'},
     {'name' : 'IBM', 'city' : 'Raleigh','state' : 'NC'}
 ]
}



The subscribe="/table" attribute specifies a topic that events can be sent to. Publish and subscribe events can be used to tie widgets together (more on this later).

RESTful  Web Services with JAX-RS


The dataTable's serviceattribute references the URI  webresources/customers/jMakiTablefor thecustomers jMakiTable  RESTful web service.  The customers RESTful web service was generated using Netbeans 6.1 as explained in the Generating RESTful Web Services from Entity Classes  tutorial.  Using Netbeans 6.1 you can generate JPA Entity Classes from Database tables, then you can Generate RESTful Web Services from Entity Classes, and then you can test the Web Services with a browser interface. The customers RESTful web service was generated from the customer data table which comes already created in the Java DB with Netbeans.  I added the jMakiTable method to the generated customers Web Service,  in order to return the customers in the jMaki table format. I followed the jMakiBackend example which comes with Jersey (the JAX-RS reference implementation) which is expained in Japods blog:  jMaki Widgets Talking To Jersey Resources In JSON.


Below is a snippet from the CustomersResource.java class which was generated by the Netbeans "Generate RESTful Web Services from Entity Classes" feature :

        
Code Sample from: CustomersResource.java

// Service URI path "/customers/"

@Path("/customers/")

public class CustomersResource{
  
    @GET
    @ProduceMime("application/json")
    public
CustomersConverterget(@QueryParam("start")
           @DefaultValue("0") int start, @QueryParam("max")
            @DefaultValue("4") int max, @QueryParam("expandLevel")
            @DefaultValue("1") int expandLevel, @QueryParam("query")
            @DefaultValue("SELECT e FROM Customer e") String query) {
        try {
            CustomersConverter custs = new CustomersConverter(
               getEntities(start, max, query),
                context.getAbsolutePath(), expandLevel);
            return
custs;
        } finally {
            PersistenceService.getInstance().close();
        }
    }


The CustomersResourcerepresents a list of customers. The CustomersResource getmethod returns a list of Customer objects in JSON format. 
  • To address a resource in REST you specify its URI. @Path is a JAX-RS annotation that identifies the URI path for the resource. For the CustomersResource  the URI path is /customers/.
  • @GET specifies that thegetmethod supports the HTTP GET method.
  • @ProduceMimespecifies the MIME types that a method can produce. Here, the annotation specifies that the getmethod returns a JSONArray object.  TheCustomersConverterclass is a JAXB annotated class which is used to marshal a list of Customer objects into XML or JSON format.   The getEntitiesmethod returns a list of Customer entity objects and is explained below.  
  • @QueryParamspecifies input parameters for methods.  When the method is invoked, the input value will be injected into the corresponding input argument. 
  • @DefaultValuespecifies a default value for an arguement if no input value is given.
Here is an example of an HTTP request for this Web Service:

    
Request: GET http://host/jMakiRest/webresources/customers/?start=0


Here is an example of an HTTP response for this Web Service:

    
Received:
{"customers":
  {"@uri":"http://host/jMakiRest/webresources/customers/",
   "customer":[
     {"@uri":"http://host/jMakiRest/webresources/customers/1/",
       "name":"JumboCom",
      "city":"Fort Lauderdale",     
       "state":"FL",
       "zip":"33015"},
     {"@uri":"http://host/jMakiRest/webresources/customers/2/",
       "name":"Livermore Enterprises",
       "city":"Miami",
       "state":"FL",
       "zip":"33055"}
    ]
  }
}


Below is the getTablemethod  from the CustomersResource.java class,  which returns a list of Customers in the jMaki  JSON table format.

        
Code Sample from: CustomersResource.java

public class CustomersResource{
     . . .
  
    @GET
    @Path("/jMakiTable")
    @ProduceMime("application/json")
    public CustomerTableModel getTable(@QueryParam("start")
           @DefaultValue("0") int start, @QueryParam("max")
            @DefaultValue("4") int max, @QueryParam("expandLevel")
            @DefaultValue("1") int expandLevel, @QueryParam("query")
            @DefaultValue("SELECT e FROM Customer e") String query) {

        CustomersConverter custs = get(start, max,
                   expandLevel, query);
        return new CustomerTableModel(custs.getCustomer());
    }


The getTablemethod calls the CustomersResourcegetmethod, explained above,  to get a list of Customer Entities which are used to create a CustomerTableModelclass. The CustomerTableModelclass is a JAXB annotated class, used to marshal a list of Customer objects into the jMaki  JSON table format.  A snippet of the CustomerTableModelclass is shown below:


        
Code Sample from: CustomerTableModel.java

@XmlRootElement
public class CustomerTableModel {

    public static class JMakiTableHeader {

        public String id;
        public String label;

        public JMakiTableHeader(String id,
           String label) {
            this.id = id;
            this.label = label;
        }
    }
    public List<JMakiTableHeader> columns =
          initHeaders();
    public List<CustomerConverter> rows;
  
    . ..



Java Persistence Query API

The CustomersResourcegetEntities method uses the Java Persistence API Queryobject to return a list of customers.

        
Code Sample from: CustomersResource.java

@Path("/customers/")

public class CustomersResource{
  
    . . .

    protected Collection<Customer> getEntities(int start, int max, String query) {
       
PersistenceService ps =PersistenceService.getInstance();
        Query query = ps.
createQuery(query);
        query.
setFirstResult(start);
       query.setMaxResults(max);
        returnquery.getResultList();
    }

 

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: query.setMaxResults(int maxResult) sets the maximum number of results to retrieve.query.setFirstResult(int startPosition) sets the position of the first result to retrieve.

In the code below, we show the Customerentity class which maps to the  CUSTOMER table that stores the customer instances. This is a typical Java Persistence entity object. There are two requirements for an entity:
  1. annotating the class with an @Entityannotation.
  2. 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. 
For more information on Netbeans and JPA seehttp://www.apress.com/book/bookDisplay.html?bID=10093 basics of developing a web application using Java™ Persistence API.

           
Code Sample from: Customer.java

@Entity

public class Customerimplements Serializable {

   
@Id 
    private Integer customerId;

    private String name;
    private String addressline1;   
    private String city;  
    private String state; 
    private String zip;

    
    public
Customer() { }
     
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }



}   


jMaki Publish Subscribe events 

jMaki publish subscribe events tie widgets actions together. The sample app uses two jMaki yahoo.buttonwidgets which publish to the /button/previous,/button/next topicswhen the respective button is clicked:

        
Code Sample from: List.jsp

<a:widget name="yahoo.button" value="{label : '<<',
                 action : {topic : '/button/previous'}}" />

<a:widget name="yahoo.button" value="{label : '>>',
                 action : {topic : '/button/next'}}" />


Events in jMaki are handled by jMaki Glue , whichhttps://ajax.dev.java.net/introGlue.html allows JavaScript components to talk to each other. You put function listeners which Subscribe to topics that your widgets Publish to in a file called glue.js (to read more about this see A practical guide to jMaki Events ).

Connecting the listener to the handler
 
The listener handler for the  /button/nexttopic is shown below. First you declare the topic to listen to and then the listener function which will handle the notification. The/button/nextlistener handler  increments the page number and then calls the getNextPagefuntion.

        
Code Sample from: glue.js
var page= 0;
var start= 0;
var batchSize=4;

jmaki.subscribe("/button/next", function(args) {
    page =page + 1;
    getNextPage(page);
});

jmaki.subscribe("/button/previous", function(args) {
    page =page - 1;
    if (page < 0) page = 0;
    getNextPage(page);
});

function getNextPage(page) {
    start = page * batchSize;
    jmaki.doAjax({method: "POST",
        url: "webresources/customers/?start="+encodeURIComponent(start),
        callback :function(req) {
           var respObj = eval('('+ req.responseText +')');
            var rows =  respObj.customers.customer;
           jmaki.publish("/table/clear", { });
            for(j=0;j<rows.length;j++) {
                var row = rows[j];
               jmaki.publish("/table/addRow",{
                   value: row
                });
            }
           
          }
    });    
}
       

The getNextPagefunction uses  jmaki.doAjax, which provides an easy way to make an  XMLHttpRequest, to call the /customers/RESTful Web Service  passing the start index  as a URI parameter.  The  callbackfunction uses  eval to convert the XMLHttpRequest response into a JSON object. Then  jmaki.publishis called to publish the returned customerJSON objects to the /table/addRowtopic.

The yahoo.dataTable widget subscribes to thetabletopic.
Subscribe events allow you to manipulate a given instance of a widget. The event names are appended to the the subscribe topic name following a "/". For example  "/table/addRow" will call the yahoo.dataTableaddRowfunction which will add the  payload value passed to the widget to the the table. This will cause the  returnedcustomerJSON object to be displayed in the table on the html page.


Conclusion
This concludes the sample application which  demonstrates a RESTful Web Service, coded using JAX-RS: Java API for RESTful Web Services (JSR-311) , which provides a list of customers, and a  jMaki  client which  gets and displays the Web Service responses in a dynamic Ajax table.

Configuration of the Application for jMaki, JPA, Netbeans 6.1 and Glassfish V2


Open and Run the Sample code:

  1. Download the sample code and extract its contents. You should now see the newly extracted directory as<sample_install_dir>/jmakiRest, where <sample_install_dir> is the directory where you installed the sample package. For example, if you extracted the contents to C:\ on a Windows machine, then your newly created directory should be atC:\jmakiRest.

  2. Start the NetBeans IDE. Click Open Project in the File menu and select the jmakiRest directory you just unzipped.

  3. Build the project as follows:

     
    • Right click the jmakiRest node in the Projects window.
    • Select Clean and Build Project.

  4. Run the project as follows:

     
    • Right click the jmakiRest node in the Projects window.
    • Select Run Project.
When you run the project, your browser should display the opening page of the Sample Application (at http://localhost:8080/jmakiRest/).


If you want to create your own jMaki application: 

References:




Sample Store Catalog using using Groovy and Grails and the Java Persistence API on Glassfish with MySQL


I modified this Groovy and Grails  Catalog Sample application to use JPA entity java classes instead of  Groovy domain classes. I followed the steps in this InfoQ article Grails + EJB Domain Models Step-by-Step  and I was really surprised at how easy it was !

download Catalog sample code

Overview of the Technologies and Frameworks in the Sample Application

The Java Persistence API provides a POJO-based persistence model for Java EE and Java SE applications. It handles the details of how relational data is mapped to Java objects, and it standardizes Object/Relational (O/R) mapping.

Grails aims to bring the "coding by convention" paradigm to Groovy. It's an open-source web application framework that leverages the Groovy language and complements Java Web development.

Groovy is an agile anddynamic language for the Java Virtual Machine, itcompiles to Java bytecode, and it combines popular features from languages such as Smalltalk, Python, and Ruby.

Grails is a Model-View-Controller based framework that simplifies the development of  web applications by reducing the need for configuration files and by generating a lot of the things needed in a database-backed Web application.

mvc.gif



The Sample Application

The sample application displays an online catalog of pets sold in a pet store. The image below shows the Catalog Listing page, which allows a user to page through a list of items in a store.

  listpet.jpg


The Model - JPA Entity Classes

The Model is your application's persistent business domain objects. A JPA Entity instance represents a row in a database table. Item is an Entity class -- a typical Java Persistence entity object -- which maps to an ITEM table that stores the item instances.

The Itemclass has a many-to-one relationship with the Addressclass,  this is specified using the @ManyToOne annotation in theItemclass and the @OneToMany(mappedBy = "address")annotation in the Addressentity class shown below:


      
        
Code Sample from: model\Item.java
package model;

// import ....

@Entity
@Table(name = "item")
public class Item implements Serializable{
    @Id
    private Long id;
    private String name;
    private String description;
    private String imageurl;
    private String imagethumburl;
    private BigDecimal price;
    @ManyToOne(optional = false)
    @JoinColumn(name = "address_id")
    private Address address;

    // getters and setters ...
}
   


 
        
Code Sample from: model\Address.java
package model;

// import ....

@Entity
@Table(name = "address")

public class Addressimplements Serializable{
    @Id
    private Long id;
    private String street1;
    private String street2;
    private String city;
    private String state;
    private String zip;
    private BigDecimal latitude;
    private BigDecimal longitude;
    private BigInteger version;
   @OneToMany(mappedBy = "address")
    private Collection<Item> items;
   
   
// getters and setters ...
 
}
   



classrel.gif

        
SQL  Sample for items table

CREATE TABLE item (
 id BIGINT NOT NULL,
 product_id BIGINT NOT NULL,
 name VARCHAR(30) NOT NULL,
 description VARCHAR(500) NOT NULL,
 imageurl VARCHAR(55),
 imagethumburl VARCHAR(55),
 price DECIMAL(14,2) NOT NULL,
 address_id BIGINT NOT NULL,
 primary key (id),
 foreign key (address_id) references address(id),
 foreign key (product_id) references product(id)
);
   



Using the Java Persistence API With Grails and MySQL

Entering the Grails command
> grails create-app catalog
creates a standard directory structure for a grails application named catalog.  After you have your directory structure , to use JPA entities with a grails application:
  1. copy your entity files into the application name\src\java directory,  in this case I copied the model.Item and the model.Address  java files into the catalog\src\java\model directory.
  2. copy the MySQL jdbc driver mysql-connector-java-5.1.6-bin.jar into the directory  catalog\lib .
  3. modify the DataSource.groovy file in the catalog\grails-app\conf directory to use MySQL as the data base and  the GrailsAnnotationConfiguration class to use the annotations in the JPA entities as shown below :


               
    Code Sample from: catalog\grails-app\conf\DataSource.groovy

    import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration
    dataSource {
        configClass = GrailsAnnotationConfiguration.class
        pooled = false
        driverClassName = "com.mysql.jdbc.Driver"
        username = "root"
        password = ""
        dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
    }
    hibernate {
        cache.use_second_level_cache=true
        cache.use_query_cache=true
        cache.provider_class='org.hibernate.cache.EhCacheProvider'
    }
    // environment specific settings
    environments {
        development {
            dataSource {
                dbCreate = "update"
                url = "jdbc:mysql://localhost/petcatalog"
            }
        }
        test {
            dataSource {
                dbCreate = "update"
                url = "jdbc:mysql://localhost/petcatalog"
            }
        }
        production {
            dataSource {
                dbCreate = "update"
                url = "jdbc:mysql://localhost/petcatalog"
            }
        }
    }                       
         


  4. In order for Grails to recognize the JPA Entity classes as domain classes,  add the hibernate.cfg.xmlfile shown below to the catalog\grails-app\conf\hibernate directory:


               
    Code Sample from: catalog\grails-app\conf\hibernate\hibernate.cfg.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

    <hibernate-configuration>
        <session-factory>
            <mapping package="model" />
            <mapping class="model.Item" />
            <mapping class="model.Address" />
        </session-factory>
    </hibernate-configuration>

The Controller

Entering the Grails command  (in the directory catalog) 
> grails generate-controllermodel.Item
will generate the ItemController.groovy   class for the model.Item entity class.

Controllers handle incoming http requests, interact with the model to get data and to process requests,  invoke the correct view, and direct domain data to the view for display.  In Grails, http requests are handled by Controller classes which are made up of one or more action methods that are executed on request and then either render a Groovy Server Page or redirect to another action. Grails routes requests to the controller action which corresponds to the URL mapping for the request. In Grails the default mapping from URL to action method follows this convention: http://host/app/controller/action/id .  For example the URL http://host/catalog/item/list calls thelist action method in the item controller class shown below.     Grails Scaffoldingprovides a series of standardized Controller action methods for listing, showing, creating, updating, and deleting objects of a class.  These standardized actions come with both controller logic and default view Groovy Server Pages. The ItemControllerlistaction renders a view with a paginated list of item objects.

        
Code Sample from: grails-app\controllers\ItemController.groovy

class ItemController{

 
def index = { redirect(action:list,params:params) }

  def list = {
    if(!params.max) params.max = 10
    [ itemList: Item.list( params ) ]
  }
. . .


When a URL has a controller but no action (e.g. http://localhost:8080/catalog/item/  ), Grails defaults to the index action. In theItemControllercode the  indexaction method redirects to the listaction.  The ItemControllerlistaction method calls the Item.list()method which returns an ArrayList of item objects retrieved from the item database table . If there are more than params.max objects in the table, Grails creates next and previous pagination links automatically. The itemList variable  is automatically made available to the view by the framework.

After executing code, actions usually render a GSP in the views directory corresponding to the name of the controller and action, for example the list action will render the grails-app\views\item\list.gsp .

The View

Entering the Grails command  (in the directory catalog) 
> grails generate-viewsmodel.Item
will generate the create.gsp , edit.gsp, list.gsp, show.gsp  groovy server pages for the model.Item entity class.
The view layer generates a web page, using data from domain objects provided by the controller. In Grails, the view is rendered usingGroovy Server Pages. Below is part of the list.gsp for the Catalog application (note I modified the html table format from the default generated).


        
Code Sample from: grails-app\views\item\list.gsp

<table>
   <thead>
     <tr>
        <g:sortableColumnproperty="name" title="Name" />
        <g:sortableColumn property="imagethumburl" title="Photo" />
        <g:sortableColumn property="price" title="Price" />
     </tr>
   </thead>
   <tbody>
     <g:eachin="${itemList}" status="i" var="item">
        <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
           <td>
            <g:linkaction="show" id="${item.id}">
                ${item.name?.encodeAsHTML()}</g:link>
           </td>
           <td>
             <img src="${createLinkTo(dir:'images',file:item.imagethumburl)}"/>
           </td>
           <td>${item.price?.encodeAsHTML()}</td>
        </tr>
     </g:each>
  </tbody>
 </table>

<div class="paginateButtons">
 <g:paginatetotal="${Item.count()}" />
</div>


The view uses instance variables set by the controller to access the data it needs to render the GSP.

GSP has a GroovyTagLib similar to the  JSP tag library. <g: are GroovyTags.
  

<g:sortableColumn
       The sortableColumntag renders a sortable column to support sorting in tables.

<g:each in="${itemList}" status="i" var="item">
loops through each object in the itemListvariable, which is an ordered ArrayList of Itemmodel objects,  and assigns each Itemmodel object to the itemvariable.
 
<g:link action="show" id="${item.id}">${item.name?.encodeAsHTML()}</g:link>  
the <g:link> GroovyTag creates an html anchor taghref based on the action,id, controller parameters specified. In this example it generates a link to the item/show/id action which when clicked will display the corresponding item details. For example this line will generate the following HTML for the variableitem
<a href="/catalog/item/show/2">Friendly Cat</a>
<img src="${createLinkTo(dir:'images',file:item.imagethumburl)}"/>  
The createLinkTotag generates an HTML link for the item's imagethumburl attribute. 

${item.price?.encodeAsHTML()}
displays the value of the item 's price attribute as escaped HTML text.

<g:paginate total="${Item.count()}" /> 
The paginate tag creates next/previous buttons and a breadcrumb trail to allow pagination of results using the Item.count()domain method.

The Show Action Method

In Grails the mapping for the URL http://host/item/show/1  ( http://host/controller/action/id )  will route to the showaction in the ItemControllerpassing 1 to the method as the id of theparamsparameter hash. The showaction of the ItemControllerclass is shown below. The ItemControllershowaction renders a view showing the details of the item object corresponding to the id parameter.

        
Code Sample from: grails-app\controllers\ItemController.groovy

 def show = {
   def item = Item.get( params.id)

   if(!item) {
      flash.message = "Item not found with id ${params.id}"
      redirect(action:list)
   }
   else { return [ item : item ] }
 }



The showaction method  calls the Item.get()method which queries the items table returning the iteminstance variable corresponding to the item with the attributeid(primary key) equal to the  idparameter. This is the equivalent of the following sql : select * from items where id='1' . The itemvariable is automatically made available to the Show view by the framework.

 

The Show View GSP

After executing code in the action, the showaction renders the app/views/item/show.gsp . Below is the GSP for the item show view :


        
Code Sample from: grails-app\views\item\show.gsp

<h2> Detail of item</h2>

<table>
<tbody>
    <tr class="prop">
      <td valign="top" class="name">Name:</td>                          
        <td valign="top" class="value">${item.name}</td>                          
      </tr>                   
      <tr class="prop">
        <td valign="top" class="name">
           Description:
        </td>                           
          <td valign="top" class="value">
           ${item.description}
        </td>                          
      </tr>                       
      <tr class="prop">
        <td valign="top" class="name">Imageurl:</td>                          
          <td valign="top" class="value">
            <img src="${createLinkTo(dir:'images',file:item.imageurl)}" />
        </td>                           
      </tr>                                           
      <tr class="prop">
        <td valign="top" class="name">Price:</td>                           
          <td valign="top" class="value">$ ${item.price}</td>                           
      </tr>
    <tr class="prop">
        <td valign="top" class="name">Address:</td>                           
          <td valign="top" class="value">
          ${item?.address?.street1}, ${item?.address?.city},    
            ${item?.address?.state}
        </td>                          
      </tr>                       
  </tbody>
</table>  



${item.description}
displays the value of the item 's descriptionattribute.
<img src="${createLinkTo(dir:'images',file:item.imageurl)}" />  
generates an HTML image tag for theitem's imageurl attribute.
${item?.address?.city}
displays the value of the item'saddress cityattribute.

The image below shows the resulting page for the url http://host/catalog/item/show/105, which displays the item 105's details:

     showpet.jpg

Layouts

Grails layouts  let you put common html on multiple views (for example page headers,  footers, sidebars).  Default layout templates are in the views layouts directory with a file name corresponding to the controller, or you can associate a view with a layout using the "layout" meta tag to your page: 
<meta name="layout" content="main">
To add a title and parrot image to the top of the Pet Catalog pages, I put this table in the app\views\layouts\main.gsp  layout:

        
Code Sample from: app/views/layouts/main.gsp

<table>
  <tr>
   <td>Pet Catalog</td>
   <td>
     <img src="${createLinkTo(dir:'images',file:'pet_logo.jpg')}"/>
   </td>
 </tr>
</table>




Conclusion
This concludes the sample application which demonstrates how to work with Groovy and Grails  to page through a list of  Item JPA Entities which are retrieved using Item Controller action methods, and displayed using Item ViewGSPs.

 

Setting Things Up and Running the Sample code onMySQL and Jetty:

  1. If MySQL is already installed, then download GlassFish v2 UR1. Otherwise you can also Download GlassFish v2 UR1 and MySQL co-bundle from the usual Download Page (instructions).

  2. Download and install Grails.

  3. Download the sample code and extract its contents. You should now see the newly extracted directory as<sample_install_dir>/Catalog, where<sample_install_dir> is the directory where you unzipped the sample package. For example, if you extracted the contents to C:\ on a Windows machine, then your newly created directory should be at C:\Catalog
    The file  "/Catalog/grails-app/conf/DataSource.groovy" is configured for aMySQL configuration.

  4. Start the MySQL database as follows:

     
    • > mysqld_safe --user root --console

  5. Create the pet-catalog database:

     
    • > mysqladmin create petcatalog --user root

  6. Create the tables in the MySQL pet-catalog database as follows:

     
    • shell> mysqlpet-catalog< catalog.sql
      
    • using the file catalog.sql file from the/Catalog directory.

  7. Run the project as follows:
    in a command window in the /Catalog directory enter the command
  8. > grails run-app
    
    This will run the Application using the built-in Jetty Servlet engine.
When you run the project, your browser should display the Catalog home page at http://localhost:8080/catalog/ .


Run the Sample code on Glassfish:
  1. Use the  WAR file in<sample_install_dir>/Catalog/Catalog.war or Create a WAR file:
     
    • > grails war
      
  2. Copy the WAR file (catalog-0.1.war) to  your Glassfish installation "domains/domain/autodeploy" directory. (Start Glassfish and MySQL if you haven't already)

  3. Enter the URL  http://localhost:8080/catalog-0.1/  in your browser, you should see the home page of the Sample Application.

For more information:



A Comet Slideshow example using dojo, Comet, Bayeux, on Grizzly with Glassfish

ajax comet bayeux    grizzly    glassfish 
This Sample Slideshow app demonstrates the usage of the dojo Ajax framework, Comet, Bayeux, with Grizzly and Glassfish.

Download the dojo Comet Sample Application Code

dojo  is an open source DHTML toolkit written in JavaScript. It includes many utilities that go beyond Ajax, for example the dojox.comet module simplifies programming comet applications. Comet is a term coined by Alex Russell  to describe applications where the Server pushes data to the client.  For example in the diagram below on the left you see  Ajax polling which uses synchronous requests/responses to get events from the server. Comet  uses long-lived previously-opened HTTP connections to "push" data to the client at any time, not only in response to user input.

comet http
Grizzly is an HTTP framework which uses the Java™ NIO API to provide fast HTTP processing . Grizzly provides Comet (long-lived streaming HTTP connections) support built on top of Grizzly's Asynchronous Request Processing (ARP).  With Grizzly ARP,  each Comet request isn't holding onto a thread which gives scalability.   Bayeuxis a protocol for routing JSON encoded events between clients andservers in a publish subscribe model.  Grizzly provides an implementation of Bayeux, which makes it really easy to build Comet applications with dojo, you just configure Glassfish for Comet and configure your Web Application's web.xml for the Grizzly  Bayeux servlet  then you can use the dojox cometd publish and subscribe methods to send and receive Comet events as described in more detail below. 

http://blogs.sun.com/carolmcdonald/resource/grizzly2.gif


Grizzly comes with Glassfish , or it can be used separately. To use Comet with Glassfish you just need to add the bold red line to the Glassfish config  domain.xml:           
Code Sample from:  index.html
<http-listener acceptor-threads="1" address="0.0.0.0"
  blocking-enabled="false" default-virtual-server="server"
  enabled="true" family="inet" id="http-listener-1" port="8080"
  security-enabled="false" server-name="" xpowered-by="true">
  <property name="cometSupport" value="true"/>
</http-listener>

Enabling Bayeux in GlassFish

to enable Bayeux on Glassfish, add the following to your Web application web.xml :

         
Code Sample from:  index.html

<servlet>
   <servlet-name>Grizzly Cometd Servlet</servlet-name>
   <servlet-class>
    com.sun.grizzly.cometd.servlet.CometdServlet
   </servlet-class>
   <init-param>
      <description>
expirationDelay is the long delay before a request is
   resumed. -1 means never.
      </description>
      <param-name>expirationDelay</param-name>
      <param-value>-1</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
   <servlet-name>Grizzly Cometd Servlet</servlet-name>
  <url-pattern>/cometd/*</url-pattern>
</servlet-mapping>

Package your war and deploy it on Glassfish,  then every request sent to your war's context-path/cometd/will be serviced by the Grizzly Bayeux runtime.

Explanation of the usage of dojox cometd in the sample Slideshow Application

I modified the comet chat example fromhere (originally written by Greg Wilkins), to share a slideshow presentation among all subscribed clients. The image below shows the Comet Slideshow page, which allows the users to share a Slideshow and chat at the same time.

http://blogs.sun.com/carolmcdonald/resource/cometslideshow.jpg



Quick installation and use of dojo with Netbeans

There are 3 ways to install dojo which you can read about at in the book of dojo. A quick and easy way to use dojo with Netbeans is to download the JavaScript libraries from http://dojotoolkit.org/downloads.   Create a new NetBeans Web Applications project. Extract the dojo toolkit  into the project web directory: .../web , then rename dojo-release-1.1.1/ to src/  this will give you the project structure shown below.  I have already done this for the sample project so you do not have to download dojo in order to run the sample.
http://blogs.sun.com/carolmcdonald/resource/dojonet.JPG

Loading base dojo and required modules into an application

In order to load dojo into your application,  put the relative path to the dojo.js file in a script element in the head section of your  HTML page as shown below:

        
Code Sample from:  index.html

<script type="text/javascript" src="src/dojo/dojo.js"></script>
<script type="text/javascript" src="chat.js"></script>



This script element will load the base dojo script which gives you access to all the dojo functionality. The rest of the Java Script for this application is in the file chat.js.

Next in chat.js the application specifies which  dojo modules to load, using  the dojo.require function (kind of like  import in Java):

        
Code Sample from:  chat.js

dojo.require("dojox.cometd");



Dojo is organized into three major layers: Dojo Core, Dijit, and DojoX.   DojoX builds on  Dojo Core and provides newer extensions to the Dojo toolkit. DojoX cometd  implements a Bayeux protocol client for use with a Bayeux server.

Initializing a connection between the dojo client and the Grizzly BayeuxServlet

When a user first loads the slideshow application, he can enter a username and join a slideshow session.

http://blogs.sun.com/carolmcdonald/resource/page1.jpg

When a user clicks on the Join button, the joinjavascript function is called.  In the joinfunction, the call to dojox.cometd.init initialises a connection to the given Comet server, in this case with the Glassfish Grizzly Bayeux servlet (note/cometd/* is the url-pattern for the Grizzly Cometd Servlet configured in the web.xml for the application).

        
Code Sample from:  chat.js
var room = {
 ...
 join: function(name){
  
   dojox.cometd.init(
"/cometd");
   dojox.cometd.subscribe("/chat/demo", room, "_chat");
   dojox.cometd.publish("/chat/demo",
      { user: room._username,
        join: true, chat : room._username+" has joined"});
 }


The dojox.cometd.subscribe line subscribes the_chat callback function to the /chat/demochannel. Any time a message is sent to the  /chat/demo channel the_chat function will be called.
The dojox.cometd.publish line publishes the message that the user  (the name that was entered with the join button) has joined the /chat/demo channel. Subscribers   to the /chat/demo channel will get this message.

Publishing the next slide for the Comet Slideshow

When the user clicks on the "Next Slide" button shown below,  a javascript funtion is called which publishes the url for the next slide.

http://blogs.sun.com/carolmcdonald/resource/elephantslide.jpg

        
Code Sample from:  index.html
<input id="previousB" class="button" type="submit" name="previous" value="Previous Slide"/> 
<input id="nextB" class="button" type="submit" name="next" value="Next Slide"/>



When the user clicks on the Next Slide button, the  javascript function shown below is called. This function callsroom.next passing the url for the next slide. The function then increments the index for the next slide. The urls for the slides are stored in the slideUrls array shown below. 

        
Code Sample from:  widget.json
var room = {
...
  _init: function(){

    var slideUrls=[
            "/dojoComet/images/image0.jpg",
            "/dojoComet/images/image1.jpg",
            "/dojoComet/images/image2.jpg",
            "/dojoComet/images/image3.jpg",
            "/dojoComet/images/image4.jpg",
            "/dojoComet/images/image5.jpg"];
    var i=0;

   
element=dojo.byId('nextB');
    element.onclick = function(){
       room.next( slideUrls[i]);
       if (i>=
slideUrls.length){i=0;}
       else {i++;}
    }

    element=dojo.byId('previousB');
    element.onclick = function(){
       room.next( slideUrls[i]);
       if (i<=0){i=0;}
       else {i--;}
    }

  }
...


The function  room.next, shown below, callsdojox.cometd.publish to publish the next slide url (input argument)  to the /chat/demo channel. Subscribers   to the /chat/demo channel will get this message.


        
Code Sample from:   chat.js
var room = {
    ...

    next: function(text){
        dojox.cometd.publish("/chat/demo", {slide: text});
    }
    ...
}


When a message is published to a Bayeux channel on the server,  it is delivered to all clients subscribed to that channel,  in this case to the  "/chat/demo" channel . In the room.join function shown beforedojox.cometd.subscribe("/chat/demo", room, "_chat")was called  to subscribe the _chat callback function to the/chat/demo channel.   The_chat callback function, shown below,  is called  with the published message as an input argument.  The _chat callback function  updates the browser page by setting the slide dom element innerHTML to an html img tag with the slide url from the published message "<img src='" + slideUrl + "'/>" . This updates the browser page with the image corresponding to the slide URL which was published.

        
Code Sample from: chat.js 
var room = {
    ...
    _chat: function(message){
        var slide=dojo.byId('slide');
        var slideUrl=message.data.slide;
        slide.innerHTML ="<img src='" + slideUrl + "'/>";
    ...
}



Conclusion
This concludes the sample application which demonstrates the usage of the dojo Ajax framework, Comet, Bayeux, with Grizzly and Glassfish.

Running the Sample Code

The sample code  is available as a NetBeans project. You can build and run the sample code using the NetBeans IDE.

Setting Things Up

  • Download and install NetBeans 6.1 bundled with GlassFish V2
  • Alternatively you can  Downloadand install GlassFish V2 separately.
  • To use Comet with Glassfish you just need to add the bold red line to the Glassfish config  domain.xml (in the directory glassfish/domains/domain1/config ):           
    Code Sample from:  index.html
    <http-listener acceptor-threads="1" address="0.0.0.0"
      blocking-enabled="false" default-virtual-server="server"
      enabled="true" family="inet" id="http-listener-1" port="8080"
      security-enabled="false" server-name="" xpowered-by="true">
      <property name="cometSupport" value="true"/>
    </http-listener>
  • Bayeux and dojo are already configured in the sample code.

Open and Run the Sample code:

  1. Download the sample code and extract its contents. You should now see the newly extracted directory as<sample_install_dir>/dojoComet, where<sample_install_dir> is the directory where you unzipped the sample package. For example, if you extracted the contents to C:\ on a Windows machine, then your newly created directory should be atC:\dojoComet.

  2. Start the NetBeans IDE. Click Open Project in the File menu and select the dojoComet directory you just unzipped.

  3. Build the project as follows:

     
    • Right click the dojoComet node in the Projects window.
    • Select Clean and Build Project.

  4. Run the project as follows:

     
    • Right click the dojoComet node in the Projects window.
    • Select Run Project.
When you run the project, your browser should display the opening page of the Sample Application (at http://localhost:8080/dojoComet/). Open another browser and set that url to http://localhost:8080/dojoComet/  then enter a name and click on the join button in both browser windows.   Then click on the next slide button in one browser window.  Both browsers should get updated with the next slide.  

For more Information:




jMaki Presentation slides and Sample code

jMaki is a lightweight framework for creating Web 2.0 applications using standards-based technologies such as CSS, HTML, and JavaScript. I have updated theSun Tech Days  jMaki presentation I am giving in Manilla with explanatory notes for the slides.  I have also updated the Dynamic Ajax table example using jMaki and Java Persistence APIs on Glassfish on my blog. Download the presentation and sample code.
Deploy the sample code as described in the blog, try out the example, look at the code, read the slides, learn about the jMaki framework.

Sample Store Catalog using using Groovy and Grails



This Catalog Sample app demonstrates the usage of Groovy and Grails to implement pagination of data sets for a Store Catalog.
download Catalog sample code

Overview of the Technologies and Frameworks in the Sample Application

Grails aims to bring the "coding by convention" paradigm to Groovy. It's an open-source web application framework that leverages the Groovy language and complements Java Web development.

Groovy is an agile anddynamic language for the Java Virtual Machine, itcompiles to Java bytecode, and it combines popular features from languages such as Smalltalk, Python, and Ruby.

Grails is a Model-View-Controller based framework that simplifies the development of  web applications by reducing the need for configuration files and by generating a lot of the things needed in a database-backed Web application.

mvc.gif



The Sample Application

The sample application displays an online catalog of pets sold in a pet store. The image below shows the Catalog Listing page, which allows a user to page through a list of items in a store.

  listpet.jpg


The Model - Grails Domain Classes

The Model is your application's persistent business domain objects. A Grails domain object instance represents a row in a database table.  The command grails create-domain-class Item generates the Item.groovyclass shown below corresponding to the item table.

After model code generation you have to add the domain object's attributes and relationships. The Itemclass has a many-to-one relationship with the Addressclass. In Grails hasManyis the many end of a many-to-one  relationship.


      
        
Code Sample from: domain\Item.groovy

class Item {
  Long id
  String name
  String description
  String imageurl
  String imagethumburl
  BigDecimal price
  Address address
}
   


 
        
Code Sample from: domain\Address.groovy

class Address {
  Long id
  String street1
  String street2
  String city
  String state
  String zip
  float latitude
  float longitude
  static hasMany = [item:Item]
}
   



classrel.gif

        
SQL  Sample for items table

CREATE TABLE item (
 id BIGINT NOT NULL,
 product_id BIGINT NOT NULL,
 name VARCHAR(30) NOT NULL,
 description VARCHAR(500) NOT NULL,
 imageurl VARCHAR(55),
 imagethumburl VARCHAR(55),
 price DECIMAL(14,2) NOT NULL,
 address_id BIGINT NOT NULL,
 primary key (id),
 foreign key (address_id) references address(id),
 foreign key (product_id) references product(id)
);
   


Groovy with Grails dynamically generates getters and setters and the dynamic methods Item.save(), Item.delete(),  Item.list(), Item.get() to retrieve/update data from/to the db table.

Grails Object Relational Mapping (GORM) is currently built on top of Hibernate but you don't have to know Hibernate to use it ( Grails  1.1 will support  the Java Persistence API) .


The Controller

Controllers handle incoming http requests, interact with the model to get data and to process requests,  invoke the correct view, and direct domain data to the view for display.  In Grails, http requests are handled by Controller classes which are made up of one or more action methods that are executed on request and then either render a Groovy Server Page or redirect to another action. Grails routes requests to the controller action which corresponds to the URL mapping for the request. In Grails the default mapping from URL to action method follows this convention: http://host/app/controller/action/id .  For example the URL http://host/catalog/item/list calls thelist action method in the item controller class shown below.     Grails Scaffoldingprovides a series of standardized Controller action methods for listing, showing, creating, updating, and deleting objects of a class.  These standardized actions come with both controller logic and default view Groovy Server Pages. The command generate-all Item generates the Item controller and the List, Show, Create, Edit Groovy Server Pages for the Item domain object. The ItemControllerlistaction renders a view with a paginated list of item objects.

        
Code Sample from: grails-app\controllers\ItemController.groovy

class ItemController{

 
def index = { redirect(action:list,params:params) }

  def list = {
    if(!params.max) params.max = 10
    [ itemList: Item.list( params ) ]
  }
. . .


When a URL has a controller but no action (e.g. http://localhost:8080/catalog/item/  ), Grails defaults to the index action. In theItemControllercode the  indexaction method redirects to the listaction.  The ItemControllerlistaction method calls the Item.list()method which returns an ArrayList of item objects retrieved from the item database table . If there are more than params.max objects in the table, Grails creates next and previous pagination links automatically. The itemList variable  is automatically made available to the view by the framework.

After executing code, actions usually render a GSP in the views directory corresponding to the name of the controller and action, for example the list action will render the grails-app\views\item\list.gsp .

The View

The view layer generates a web page, using data from domain objects provided by the controller. In Grails, the view is rendered usingGroovy Server Pages. Below is part of the list.gsp for the Catalog application (note I modified the html table format from the default generated).


        
Code Sample from: grails-app\views\item\list.gsp

<table>
   <thead>
     <tr>
        <g:sortableColumnproperty="name" title="Name" />
        <g:sortableColumn property="imagethumburl" title="Photo" />
        <g:sortableColumn property="price" title="Price" />
     </tr>
   </thead>
   <tbody>
     <g:eachin="${itemList}" status="i" var="item">
        <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
           <td>
            <g:linkaction="show" id="${item.id}">
                ${item.name?.encodeAsHTML()}</g:link>
           </td>
           <td>
             <img src="${createLinkTo(dir:'images',file:item.imagethumburl)}"/>
           </td>
           <td>${item.price?.encodeAsHTML()}</td>
        </tr>
     </g:each>
  </tbody>
 </table>

<div class="paginateButtons">
 <g:paginatetotal="${Item.count()}" />
</div>


The view uses instance variables set by the controller to access the data it needs to render the GSP.

GSP has a GroovyTagLib similar to the  JSP tag library. <g: are GroovyTags.
  

<g:sortableColumn
       The sortableColumntag renders a sortable column to support sorting in tables.

<g:each in="${itemList}" status="i" var="item">
loops through each object in the itemListvariable, which is an ordered ArrayList of Itemmodel objects,  and assigns each Itemmodel object to the itemvariable.
 
<g:link action="show" id="${item.id}">${item.name?.encodeAsHTML()}</g:link>  
the <g:link> GroovyTag creates an html anchor taghref based on the action,id, controller parameters specified. In this example it generates a link to the item/show/id action which when clicked will display the corresponding item details. For example this line will generate the following HTML for the variableitem
<a href="/catalog/item/show/2">Friendly Cat</a>
<img src="${createLinkTo(dir:'images',file:item.imagethumburl)}"/>  
The createLinkTotag generates an HTML link for the item's imagethumburl attribute. 

${item.price?.encodeAsHTML()}
displays the value of the item 's price attribute as escaped HTML text.

<g:paginate total="${Item.count()}" /> 
The paginate tag creates next/previous buttons and a breadcrumb trail to allow pagination of results using the Item.count()domain method.

The Show Action Method

In Grails the mapping for the URL http://host/item/show/1  ( http://host/controller/action/id )  will route to the showaction in the ItemControllerpassing 1 to the method as the id of theparamsparameter hash. The showaction of the ItemControllerclass is shown below. The ItemControllershowaction renders a view showing the details of the item object corresponding to the id parameter.

        
Code Sample from: grails-app\controllers\ItemController.groovy

 def show = {
   def item = Item.get( params.id)

   if(!item) {
      flash.message = "Item not found with id ${params.id}"
      redirect(action:list)
   }
   else { return [ item : item ] }
 }



The showaction method  calls the Item.get()method which queries the items table returning the iteminstance variable corresponding to the item with the attributeid(primary key) equal to the  idparameter. This is the equivalent of the following sql : select * from items where id='1' . The itemvariable is automatically made available to the Show view by the framework.

 

The Show View GSP

After executing code in the action, the showaction renders the app/views/item/show.gsp . Below is the GSP for the item show view :


        
Code Sample from: grails-app\views\item\show.gsp

<h2> Detail of item</h2>

<table>
<tbody>
    <tr class="prop">
      <td valign="top" class="name">Name:</td>                          
        <td valign="top" class="value">${item.name}</td>                          
      </tr>                   
      <tr class="prop">
        <td valign="top" class="name">
           Description:
        </td>                           
          <td valign="top" class="value">
           ${item.description}
        </td>                          
      </tr>                       
      <tr class="prop">
        <td valign="top" class="name">Imageurl:</td>                          
          <td valign="top" class="value">
            <img src="${createLinkTo(dir:'images',file:item.imageurl)}" />
        </td>                           
      </tr>                                           
      <tr class="prop">
        <td valign="top" class="name">Price:</td>                           
          <td valign="top" class="value">$ ${item.price}</td>                           
      </tr>
    <tr class="prop">
        <td valign="top" class="name">Address:</td>                           
          <td valign="top" class="value">
          ${item?.address?.street1}, ${item?.address?.city},    
            ${item?.address?.state}
        </td>                          
      </tr>                       
  </tbody>
</table>  



${item.description}
displays the value of the item 's descriptionattribute.
<img src="${createLinkTo(dir:'images',file:item.imageurl)}" />  
generates an HTML image tag for theitem's imageurl attribute.
${item?.address?.city}
displays the value of the item'saddress cityattribute.

The image below shows the resulting page for the url http://host/catalog/item/show/105, which displays the item 105's details:

     showpet.jpg

Layouts

Grails layouts  let you put common html on multiple views (for example page headers,  footers, sidebars).  Default layout templates are in the views layouts directory with a file name corresponding to the controller, or you can associate a view with a layout using the "layout" meta tag to your page: 
<meta name="layout" content="main">
To add a title and parrot image to the top of the Pet Catalog pages, I put this table in the app\views\layouts\main.gsp  layout:

        
Code Sample from: app/views/layouts/main.gsp

<table>
  <tr>
   <td>Pet Catalog</td>
   <td>
     <img src="${createLinkTo(dir:'images',file:'pet_logo.jpg')}"/>
   </td>
 </tr>
</table>




Conclusion
This concludes the sample application which demonstrates how to work with Groovy and Grails  to page through a list of  Item Model objects which are retrieved using Item Controller action methods, and displayed using Item ViewGSPs.

 

Setting Things Up and Running the Sample code onMySQL and Jetty:

  1. If MySQL is already installed, then download GlassFish v2 UR1. Otherwise you can also Download GlassFish v2 UR1 and MySQL co-bundle from the usual Download Page (instructions).

  2. Download and install Grails.

  3. Download the sample code and extract its contents. You should now see the newly extracted directory as<sample_install_dir>/Catalog, where<sample_install_dir> is the directory where you unzipped the sample package. For example, if you extracted the contents to C:\ on a Windows machine, then your newly created directory should be at C:\Catalog
    The file  "/Catalog/grails-app/conf/DataSource.groovy" is configured for aMySQL configuration.

  4. Start the MySQL database as follows:

     
    • > mysqld_safe --user root --console

  5. Create the pet-catalog database:

     
    • > mysqladmin create petcatalog --user root

  6. Create the tables in the MySQL pet-catalog database as follows:

     
    • shell> mysqlpet-catalog< catalog.sql
      
    • using the file catalog.sql file from the/Catalog directory.

  7. Run the project as follows:
    in a command window in the /Catalog directory enter the command
  8. > grails run-app
    
    This will run the Application using the built-in Jetty Servlet engine.
When you run the project, your browser should display the Catalog home page at http://localhost:8080/catalog/ .


Run the Sample code on Glassfish:
  1. Use the  WAR file in<sample_install_dir>/Catalog/Catalog.war or Create a WAR file:
     
    • > grails war
      
  2. Copy the WAR file (catalog-0.1.war) to  your Glassfish installation "domains/domain/autodeploy" directory. (Start Glassfish and MySQL if you haven't already)

  3. Enter the URL  http://localhost:8080/catalog-0.1/  in your browser, you should see the home page of the Sample Application.

References



 

Speaking at CommunityOne 2008

Kito Mann and I  are speaking at CommunityOne 2008 on: "Examining a Sample Application Built in Three Ways: Java™ Platform, Enterprise Edition (Java EE Platform), Spring 2.5, and Seam 2.0 Deployed on the GlassFish™ Project". CommunityOne is the day before JavaOne (May 05), its free, and you can learn about a wide range of open source projects.
Our talk is May 05 12:25 - 13:20    Moscone South - Esplanade 305. Here is the abstract:
This session looks at the implementation of the same web application developed with three open source frameworks and highlights what was done differently with each one. First it explains the implementation of the sample application with JavaServer™ Faces and Enterprise JavaBeans™ 3.0 (EJB™ 3.0) technology and the Java™ Persistence API (JPA). Next it looks at how this application was developed with JavaServer Faces technology, the JPA, and Spring 2.5. Finally, it looks at this application developed with JavaServer Faces and EJB 3.0 technology, the JPA, and Seam 2.0. The presentation highlights differences in the frameworks, such as the Seam context model for stateful components. All three versions were deployed on GlassFish™ project V2, and the source code is available in my blog.

http://technorati.com/tag/JavaOne 
C1_170x93_SpeakerB.gif

 



 

Metro Hands-on Lab at JavaOne 2008

Fabian Ritzman, Martin Grebac and I  have developed a hands-on lab on Metro Web Services for JavaOne 2008. At JavaOne hands-on labs, you bring your own laptop, this allows you to easily take home and reuse the stuff you learn in the lab. We have a step by step lab doc with screenshots etc. that you can work through to familiarize yourself with Metro. Fabian and I will briefly explain and demonstrate the lab exercises, then you will complete them on your laptop. There will be proctors to help with any questions. Hope to see you there!  You can find out more about the lab here: https://www28.cplan.com/cc191/session_details.jsp?isid=296941&ilocation_id=191-1&ilanguage=english

The title of the lab is "Metro: Try Out Simple and Interoperable Web Services". Session ID is 3410. The lab takes place on Tuesday, 10:50 - 12:50.  Here is the complete abstract:

Metro is a high-performance, extensible, easy-to-use web service stack. You can use it for every type of web service, from simple to reliable, secured, and transacted web services that interoperate with .NET services. Metro bundles stable versions of the JAX-WS (Java™ API for XML Web Services) reference implementation and WSIT (Web Services Interoperability Technology).

JAX-WS is a fundamental technology for developing SOAP-based and RESTful Java technology-based web services. WSIT enables secure, reliable interoperability between Java technology-based web services and Microsoft’s Windows Communication Foundation.

This Hands-on Lab starts by developing a simple Metro web service and showing how to enhance this web service with Metro features such as reliability and security. The next part of the lab enables a web service client with Metro security features and has it interoperate with the previously built service. The lab shows the ease of development the NetBeans™ 6.0 release provides for achieving this.

The lab uses the NetBeans 6.0 release to modify and configure both the web service and the client, using Sun’s GlassFish™ project application server as the container. The lab uses WS-Reliability and WS-Security as examples of Metro’s secure, reliable features.

The lab comprises the following sections:

Introduction to Metro 
  • Develop and deploy a basic catalog web service to return a list of catalog items
  • Test the web service, using the Tester application provided by the GlassFish project
Metro Reliability 
  • Enable reliability on the catalog web service, and examine the messages
  • Develop and deploy a Metro client for the catalog web service, and configure the client for reliable access to the web service
Metro Security 
  • Enable security on the catalog web service, and examine the messages
  • Configure the Metro client for the catalog web service (from the previous exercise) for secure access to the secure web service

Prerequisites: some understanding of Servlets, XML, and SOAP

At JavaOne, this lab will be presented in Hall E (Room# 132).

Please bring your laptops to this lab as there no machines provided in this room.

System requirements: 
  • Supported OS: Windows 2000/XP, Solaris 10/11, Linux
  • Memory requirement: 768MB minimum, 1GB recommended
  • Disk space requirement: 300 MB

Software requirements:

Also please make sure to install the following software prior to coming to this lab: 
  • JDK 5.0 or 6
  • NetBeans 6.0.1 with Web & Java EE pack
  • GlassFish V2 UR1
Tags: Web Services, Metro,JavaOne 
170x93_Speaker_v4.gif

 



I'm deeply saddened to share the news that Steve Metsker passed away last Friday.
Steve Metsker was a member of and speaker for the Richmond JUG. Steve was also the author of the following books: Design Patterns in C#, Design Patterns Java Workbook, Building Parsers in Java.

Steve, his wife Alison, my husband and I enjoyed sharing our experiences about working and living in Switzerland. There was a memorial service Sunday which was overflowing with people, many from the local IT community. He will be greatly missed.

The following is from his family:
Steven John Metsker passed away on Friday, February 8, 2008 after a short illness with cancer. Aged 49, he is survived by his loving wife, Alison, and their daughters, Sarah-Jane and Emma-Kate. Having earned Engineering degrees from both the Colorado State University and the University of Massachusetts, Steve was accomplished in a profession for which he had great passion. Born in Colorado, he also lived in Massachusetts, Maine, Texas, Switzerland, Kentucky, England, and Richmond. He made many friends everywhere he went. A loving and caring husband, father, brother, son, friend, and colleague, he was also a humble and wise mentor. He was a positive and loving influence on all of us, who will miss him dearly. In lieu of flowers please send donations to St. Baldrick's fund raising program for childhood cancer or to Our Lady's Children's Hospital, Dublin Ireland.

a Dynamic Ajax table example using jMaki and Java Persistence APIs on Glassfish


This Sample Catalog app demonstrates the usage of the Java Persistence APIs to implement server side pagination (recommended for large sets of data), and  jMaki to get and display the results in a dynamic Ajax table.

Download the jMaki Sample Application Code

https://ajax.dev.java.net/jMaki is an Ajax framework that provides a lightweight model for creating JavaScript centric Ajax-enabled web applications. jMaki provides wrappedwidgets that can be used as JavaServer Pages tags, as JavaServer Faces components, within a Phobos application, or with PHP. This sample applicaton uses jMaki with JavaServer Pages.

Explanation of the usage of jMaki and the Java Persistence APIs in a sample Catalog Application

The image below shows the Customer Listing page, which allows the user to page through a list of customers.

pagingtable.jpg

jMaki dataTable widget

With  jMaki and JavaServer Pages, you can easily include wrapped widgets from ajax toolkits into a JavaServer Page as a custom JSP tag. With the Netbeans jMaki plugin you can drag  jMaki widgets from the Palette into a JSP. jMaki standardizes widget data and event models to simplify the programming model and to simplify interactions between widgets.

The sample application's index.jsp page uses a jMaki yahoo.dataTable widget to display a  list of customers in a dynamic table.

The jMaki table widgets (there is also a jMaki dojo table widget) are useful when you want to show a set of results in tabular data on a web page.  Table widgets provide sortable columns, row selection, and they can be updated using jMaki publish subscribe events.

In the List.jsp web page the dataTable is defined as shown below:   (Note:Redcolors are for jMaki tags or variables,  and Green for my code orvariables)

        
Code Sample from:  index.jsp

<a:widget name="yahoo.dataTable"
          subscribe="/table"
          service="CatalogService"/>


To determine the data format and events for the table you can refer to the  jMaki Table Data Model or look at the widget.json file for the table widget. This file is located in the resources/yahoo/dataTable directory.
The serviceattribute references the CatalogServiceservlet which returns the data to be included in the table.  The data for the table should be a JSON object containing an object of columns and an array of row arrays. The column names need a unique id which is then used in the data to associate it with a given row. An example for a table of companies is shown below:


        
Code Sample from:  widget.json
{
 'columns':[
     {'label' :'Company', 'id' : 'name'},
     {'label':'City', 'id' : 'city'}
 ],
 'rows':[
     {'name' : 'Sun Microsystems', 'city' : 'Santa Clara'},
     {'name' : 'IBM', 'city' : 'Raleigh'}
 ]
}



The publishsubscribeattributes specify a topic that publish and subscribe events will be sent to. Publish and subscribe events can be used to tie widgets together (more on this later).

The dataTable's service="CatalogService"  calls the CatalogServiceservlet which calls the getCustomersJSONmethod of the Catalogclass:


        
Code Sample from: Catalog.java
public class Catalog {
  
 public List<Customer> getCustomers()throws Exception {
   EntityManager em = getEntityManager();
   Query q = em.createQuery(
          "select object(o) from Customer as o");
   q.setMaxResults(batchSize);
   q.setFirstResult(firstItem);
   return q.getResultList();
 }  

 public JSONArraygetCustomersJSON()throws Exception {
   JSONArraycustomersJSON = new JSONArray();
   List<Customer> customers = getCustomers();
   for (Customer customerData : customers) {
       JSONObject customerJSON = customerData.toJSON();
       customersJSON.put(customerJSON);
   }
   return customersJSON;
 }



Java Persistence Query API

The Catalog getCustomersJSON() uses the Java Persistence API Queryobject to return a list of customers, a JSONArray  object is used to return the list in JSON format.  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: query.setMaxResults(int maxResult) sets the maximum number of results to retrieve.query.setFirstResult(int startPosition) sets the position of the first result to retrieve.

In the code below, we show the Customerentity class which maps to the  CUSTOMER table that stores the customer instances. This is a typical Java Persistence entity object. There are two requirements for an entity:
  1. annotating the class with an @Entityannotation.
  2. 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. 
For more information on Netbeans and JPA seehttp://www.apress.com/book/bookDisplay.html?bID=10093 basics of developing a web application using Java™ Persistence API.

           
Code Sample from: Customer.java

@Entity

public class Customerimplements Serializable {

   
@Id 
    private Integer customerId;

    private String name;
    private String addressline1;   
    private String city;  
    private String state; 
    private String zip;

    
    public
Customer() { }
     
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }


    ...
    public
JSONObjecttoJSON() {
        JSONObject thisJSON=new JSONObject();
        thisJSON.put("name", this.getName());
        thisJSON.put("city", this.getCity());
        thisJSON.put("state", this.getState());
        thisJSON.put("zip", this.getZip());
    }

}   


I added thetoJSON() method to theCustomer to return a JSON representation of the Customer entity.

jMaki Publish Subscribe events 

jMaki publish subscribe events tie widgets actions together. The sample app uses two jMaki yahoo.buttonwidgets which publish to the /button/previous,/button/next topicswhen the respective button is clicked:

        
Code Sample from: List.jsp

<a:widget name="yahoo.button" value="{label : '<<',
                 action : {topic : '/button/previous'}}" />

<a:widget name="yahoo.button" value="{label : '>>',
                 action : {topic : '/button/next'}}" />


Events in jMaki are handled by jMaki Glue , whichhttps://ajax.dev.java.net/introGlue.html allows JavaScript components to talk to each other. You put function listeners which Subscribe to topics that your widgets Publish to in a file called glue.js (to read more about this see A practical guide to jMaki Events ).

Connecting the listener to the handler
 
The listener handler for the  /button/nexttopic is shown below. First you declare the topic to listen to and then the listener function which will handle the notification. The/button/nextlistener handler  increments the page number and then calls the getNextPagefuntion.

        
Code Sample from: glue.js
var page= 0;

jmaki.subscribe("/button/next", function(args) {
    page =page + 1;
    getNextPage(page);
});

jmaki.subscribe("/button/previous", function(args) {
    page =page - 1;
    if (page < 0) page = 0;
    getNextPage(page);
});

function getNextPage(page) {
    jmaki.doAjax({method: "POST",
        url: "CatalogService?page="+encodeURIComponent(page)+
           "&rowsonly=" + encodeURIComponent(rowsonly),
        callback :function(req) {
           customers = eval(req.responseText);
           jmaki.publish("/table/clear", { });
           jmaki.publish("/table/addRows",
                   { value : customers} );
        }
    });    
}
       

The getNextPagefunction uses  jmaki.doAjax, which provides an easy way to make an  XMLHttpRequest, to call the CatalogServiceservlet passing the page number as a URI parameter.  The callbackfunction uses  eval to convert the XMLHttpRequest response into a JSON object. Then  jmaki.publishis called to publish the returned customersJSON object to the /table/addRowstopic.

The yahoo.dataTable widget subscribes to thetabletopic.
Subscribe events allow you to manipulate a given instance of a widget. The event names are appended to the the subscribe topic name following a "/". For example  "/table/addRows" will call the yahoo.dataTableaddRowsfunction which will add the  payload value passed to the widget to the the table. This will cause the  returnedcustomersJSON object to be displayed in the table on the html page.

This CatalogServletprocessRequest  method is defined as shown below:

        
Code Sample from: CatalogBean.java

public class CatalogServletextends HttpServlet {

protected void processRequest(HttpServletRequest request,
       HttpServletResponse response)
       throws ServletException, IOException {

   Catalog catalog = new Catalog();
   response.setContentType("text/plain;charset=UTF-8");
   PrintWriter out = response.getWriter();
   int page =
     Integer.parseInt(request.getParameter("page"));
   int rowsonly =
     Integer.parseInt(request.getParameter("rowsonly"));


   JSONArray array = catalog.getNextCustomersJSON(page);
   if (rowsonly == 1) {
     out.println(array.toString());
   } else{
     out.println("{columns : [" +
        "{ label : 'Company', id : 'name'}," +
        "{ label :'City', id : 'city'}," +
        "{ label : 'State', id : 'state'}," +
        "{ label : 'Zip', id : 'zip'}" +
        "],");
     out.println("rows: ");
     out.println(array.toString());
     out.println(" }");
   }
   out.close();
}


The CatalogServletsimply calls the Catalog class to get the next list of results from the database like we saw in the previous code. The CatalogServletthen returns the resulting JSONArray as a text string.

Conclusion
This concludes the sample application which demonstrates the usage of the Java Persistence APIs and jMaki in a dynamic Ajax table example.

Configuration of the Application for jMaki, JPA, Netbeans 6.1 and Glassfish V2


Open and Run the Sample code:

  1. Download the sample code and extract its contents. You should now see the newly extracted directory as<sample_install_dir>/JPAjmaki, where <sample_install_dir> is the directory where you installed the sample package. For example, if you extracted the contents to C:\ on a Windows machine, then your newly created directory should be atC:\JPAjmaki.

  2. Start the NetBeans IDE. Click Open Project in the File menu and select the JPAjmaki directory you just unzipped.

  3. Build the project as follows:

     
    • Right click the JPAjmaki node in the Projects window.
    • Select Clean and Build Project.

  4. Run the project as follows:

     
    • Right click the JPAjmaki node in the Projects window.
    • Select Run Project.
When you run the project, your browser should display the opening page of the Sample Application (at http://localhost:8080/JPAjmaki/).


If you want to create your own jMaki application: 

References:




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  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 Seam Refresh  and the clickable list example in the Seam Tutorial

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

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 rendererdisplays the data as an HTML table.

In the List.jsp web page the dataTable is defined as shown below:   (Note:Redcolors are for Java EE tags, annotations code, Blue for Seamspecific and Green for my code orvariables)

        
Code Sample from:  List.jsp

<h:dataTablevalue='#{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 valueattribute 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 itemsattribute of the CatalogBeanclass:


        
Code Sample from: CatalogBean.java

@Stateful

@Scope(SESSION)
@Name("catalog")
@Interceptors({org.jboss.seam.ejb.SeamInterceptor.class})

public class CatalogBeanimplements Serializable, Catalog {
   
    @DataModel
    private List<Item> items=null;

    @Factory("items")
    public void getItems() {
        if ((items==null)  || (index != firstItem) ){
           getNextItems();
        }
    }
   



The @DataModelSeam annotation exposes an attribute of type java.util.List to a JSF page as an instance of javax.faces.model.DataModel. The  <h:dataTable>  supports data binding to a collection of data objects represented by a DataModelinstance.  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 nameditems.

When the List.jsp page is diplayed it will try to resolve the items context variable. Since this context variable is not initialized, Seam will call the @Factorymethod getItems(), which performs a JPA query (see getNextItems() code below) and results in a DataModel being outjected.  The @Factory annotation tells Seam to invoke the getItems()method to initialize the itemsvalue.

The @Name Seam annotation specifies catalogas the application unique component name which Seam will use to resolve references to the catalogcontext variable. Seam will instantiate the component and bind a new instance to the context variable the first time JSF encounters the variable name catalog. The instance will be bound to the context specified by the@ScopeSeam annotation. The CatalogBean is a org.jboss.seam.ScopeType.SESSIONscoped component. This means that the JSF components  can bind to the catalog  managed bean without configuring this in the faces-config.xml.

The @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 @Interceptors EJB 3.0 annotation registers the SeamInterceptor.class as an EJB interceptor for this session bean component.
The Seam framework uses  EJB interceptors to perform bijection, context demarcation, validation, etc, (the interceptor could be defined in the ejb-jar.xml instead).

 

Column JSF component

On the List.jsp page the Item Name, Photo, and Price  properties are displayed with the column component:

        
Code Sample from: List.jsp
<h:dataTablevalue='#{items}' var='dataTableItem' border="1"
      cellpadding="2" cellspacing="0">
...
  <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 the dataTable component. While the dataTable component is iterating over the rows of data, it processes the column component associated with eachcolumn tag for each row in the table. As the dataTable component iterates through the list, each reference to dataTableItem points to the 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 headers, footers, and captions.

Java Persistence Query API

The CatalogBeanSession EJB uses the Java Persistence API Queryobject to return a list of items. With the @PersistenceContextannotation the CatalogBean 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).

        
Code Sample from: CatalogBean.java

  
@DataModel
   private List<Item> items=null;

   @PersistenceContext(unitName="PetCatalogPu")
   private EntityManager em;

   private int batchSize = 10;
   private int index = 0;
   private int firstItem = 0;
 
   @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
   public void getNextItems() {
        Query q = em.createQuery("select object(o) from Item as o");
        q.setMaxResults(batchSize);
        q.setFirstResult(firstItem);
        items= q.getResultList();
        index = firstItem;
   }
   

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 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: query.setMaxResults(int maxResult) sets the maximum number of results to retrieve.query.setFirstResult(int startPosition) sets the position of the first result to retrieve.

In the code below, we show the 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:
  1. annotating the class with an @Entityannotation.
  2. 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 Addressand Productare also annotated. For more information on defining JPA entities see Pro EJB 3: Java Persistence API book.

           
Code Sample from: Item.java

@Entity
@Name("item")
@Scope(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;
    @OneToOne(cascade={CascadeType.PERSIST})
    private Address address;
    @ManyToOne
    private Product product;

    
    public Item() { }
    

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
   
 


    ...
}   



The @Namehttp://docs.jboss.com/seam/1.0.1.GA/reference/en/html/annotations.html#name-annotation seam annotation specifies the (application unique) component name item, which is used in the  Detail.jsp to display the selected item's attributes. The @ScopeSeam annotation binds the iteminstance to the org.jboss.seam.ScopeType.EVENTcontext.

The CatalogBeanpages through the list of items by maintaining the firstItemand batchSizeattributes and passing these as parameters to thequery.setFirstResult(int startPosition),query.setMaxResults(int maxResult) methods. The CatalogBean's scope is defined as org.jboss.seam.ScopeType.SESSION, which corresponds to the JSF managed bean session scope.


The CatalogBeanitemCountproperty is  used to get and display the number of Catologue items in the  data base:

        
Code Sample from: List.jsp

<h:outputText value="Item #{catalog.firstItem+ 1}..#{catalog.lastItem} of
     #{catalog.itemCount}"/>



The CatalogBeangetItemCount() method uses the JPA javax.persistence.Queryinterface to get the count of all items in the database item table:

        
Code Sample from: CatalogBean.java
        
   
private int itemCount= 0;

    public int getItemCount() {
        Query q = entityManager.createQuery("select count(o) from Item as o");     
       
itemCount = ((Long)q.getSingleResult()).intValue();     
        return
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 actionevent to the application. 

        
Code Sample from: List.jsp

 <h:commandLinkaction="#{catalog.next}" value="Next #{catalog.batchSize}"
    rendered="#{catalog.lastItem + catalog.batchSize <= catalog.itemCount}"/>   


This commandLink actionattribute 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 CatalogBeannext()method is defined as shown below:

        
Code Sample from: CatalogBean.java

   public String next() {
       if (firstItem + batchSize < getItemCount()) {
           firstItem += batchSize;
           getNextItems();
       }
       return "item_list";
   }


The JavaServer Faces NavigationHandlermatches the logical outcome,  item_listagainst the navigation rules in the application configuration resource file faces-config.xmlto 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 commandLinkis  used to provide a link to click on to display the previous page of items. This commandLink actionattribute  references the  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.jsp

 <h:commandLinkaction="#{catalog.prev}" value="Previous #{catalog.batchSize}"   
        rendered="#{catalog.firstItem >=catalog.batchSize}"/>


 This CatalogBeanprev()method  is defined as shown below: 
 
        
Code Sample from: CatalogBean.java

    public String prev() {
        firstItem -= batchSize;
        if (firstItem < 0) {
            firstItem = 0;
        }
        getNextItems();
        return "item_list";
    }     


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 CatalogBean select() method:

        
Code Sample from: List.jsp

   <h:column>
       <f:facet name="header">
          <h:outputText value="Name"/>
       </f:facet>
       <h:commandLink action="#{catalog.select}" value="#{dataTableItem.name}"/>   
   </h:column>


With Seam if you use the @DataModelSelectionwith the @DataModelannotation, when the user clicks on the link, Seam will propagate the selected row from theDataModel into the annotated attribute:

        
Code Sample from: CatalogBean.java

    @DataModelSelection
    @Out(required=false)
    private Item item;

    public String select() {
        return "item_detail";
    }



The @DataModelSelectionSeam annotation tells Seam to inject the DataModel List element corresponding to the clicked link into the itemattribute.  The @Out Seam annotation transfers the value of this attribute to the itemevent context variable, making it available to a JSP page after theactioncatalog.selectmethod execution. So when a row of the dataTable is selected, the selected row is injected to the itemattribute of the CatalogBeanStateful bean, and then outjected to the event context variable named itemwhich is used in the Detail.jsp page to display the item details.

The CatalogBean select() returns a string,  "item_detail",which causes the Detail.jsp page to display the item details. The JavaServer Faces NavigationHandlermatches the logical outcome,  item_detailagainst 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 theDetail.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 outputTextcomponent to display the itemproperties:

        
Code Sample from:  Detail.jsp

    <h:outputTextvalue="#{item.name}" title="Name" />
    <h:outputText value="#{
item.description}" title="Description"/>
    <h:graphicImage url="#{item.imageurl}" title="Imageurl" />
    <h:outputText value="#{item.price}" title="Price" />
    <h:outputText value="#{item.address.city}" title="Address" />
    <h:outputText value="#{item.contactinfo.email}" title="Address"/>  




    detailpage.jpg


Conclusion
This concludes the sample application which demonstrates how to useSeam with the JSF dataTable and DataModel to page through a list of  Item Entities which are retrieved using  the CatalogBean Stateful Session EJB methods which use  the Java Persistence APIs.


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

First I recommend reading Brian Leonard's blog Seam Refresh .  I will summarize and update those steps here:

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 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 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\hibernate-annotations.jar
    • Seam \lib\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 faces-config.xml file under webpages web-inf:
     
    <lifecycle>
            <phase-listener>
                org.jboss.seam.jsf.SeamPhaseListener
            </phase-listener>
    </lifecycle>

  • add the following  context parameter to your web.xml file
    <context-param>
         <param-name>
              org.jboss.seam.core.init.jndiPattern
         </param-name>
         <param-value>
               java:comp/env/your ear name/#{ejbName}/local
         </param-value>  
    </context-param>
  • add the following listener class to yourweb.xml file
     
    <listener>
            <listener-class>
                org.jboss.seam.servlet.SeamListener
            </listener-class>
    </listener>
  • For any session EJB's referenced from the web, add  EJB references to your web.xml,for example:
     
    <ejb-local-ref>
         <ejb-ref-name>your ear name/CatalogBean/local</ejb-ref-name>
         <ejb-ref-type>Session</ejb-ref-type>
         <local-home/>
         <local>your package name.Catalog</local>
         <ejb-link>CatalogBean</ejb-link>
    </ejb-local-ref>
  • For any EJB's referenced from the web add  a Seam interceptor to the EJB, for example : @Interceptors({org.jboss.seam.ejb.SeamInterceptor.class})

References:




Filter Blog

By date: