ZK with EJB3.1 running on Glassfish

A while back I created a tutorial that covers the direct DB access from a ZK application (link). Today I want to show you the most simple ZK application that reads from a DB using EJB 3.1 and display the data in a listbox.

Prerequisites:

  • Netbeans 6.7 or later
  • Installed ZK plugin (link)
  • Glassfish V3 or later

The tutorial covers 2 steps, the first part to create an EJB and the second part to create a ZK application. It covers all details of the project creation with Netbeans, but it does not elaborate the underlying EJB, JPA technology, feel free to read more about it at the Netbeans doc (eg. link) or in-detail at Oracle (link).

Part 1 – Create EJB

  1. Create a new Java EE project, type EJB module (ZK_EJB).

    New EJB Project

    New EJB Project

    New EJB project

  2. Create an Entity Class (customerZK)
    NOTE: I recommend NOT to use customer as class name because the default Derby DB already contains a table customer !
    In our simple application we only use 3 string fields (lastName, firstName and companyName)
    We make use of the build-in Derby DB (and the existing sample DB) and Eclipse Link as Persistence Provider (the only available JPA 2.0 provider) 

    Entity Class

    Entity Class

  3. Add fields to entity class
     private String lastName;
     private String firstName;
     private String companyName;
    

    customerZK.java

    Right-click into the code | Insert Code | Getter and Setter..

    Getter and Setter

  4. Create a session facade

    Session Facade

    Session Facade

  5. Build the project
    Creates a ZK_EJB.jar in the dist folder.

Create the ZK application

  1. Create a new web application, type ZKx.y.z JavaEE6 Application (ZK_EJB_WEB)

    New Web Project

    New Web Project

  2. Add the ZK_EJB library
    (Note the library is not copied to your web project folder if you dont opt for a local library folder. Do not move your ZK_EJB project otherwise) 

    Libraries

    ZK_EJB.jar

     

  3. Modify index.zul
    Add a listbox and the link to the controller class (created in sub-sequent steps)

    <?xml version="1.0" encoding="UTF-8"?>
    
    <zk xmlns="http://www.zkoss.org/2005/zul">
     <window id="list" apply="controller.indexController" title="Customer List">
     <listbox id="lst" width="100%" >
     <listhead sizable="true">
     <listheader id="col1" label="Company"/>
     <listheader id="col2" label="First Name" />
     <listheader id="col3" label="Last Name" />
     </listhead>
     </listbox>
     </window>
    </zk>
    
  4. Create a controller class
    package controller;
    
    import ejb.customerZK;
    import ejb.customerFacade;
    import java.util.ArrayList;
    import java.util.List;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import org.zkoss.zk.ui.Component;
    import org.zkoss.zk.ui.util.GenericForwardComposer;
    import org.zkoss.zul.ListModelList;
    import org.zkoss.zul.Listbox;
    import org.zkoss.zul.Listcell;
    import org.zkoss.zul.Listitem;
    import org.zkoss.zul.ListitemRenderer;
    
    public class indexController extends GenericForwardComposer {
    
     Context context;
     customerFacade facade;
     List<customerZK> customer = null;
     private Listbox lst;
    
     @Override
     public void doAfterCompose(Component comp) throws Exception {
     super.doAfterCompose(comp);
    
     context = new InitialContext();
    
     try {
     facade = (customerFacade) context.lookup("java:global/ZK_EJB_WEB/customerFacade");
    
     // Read all records and store it in a arraylist
     customer = new ArrayList();
     customer.addAll(facade.findAll());
    
     // Set listmodel for the ZK list component
     lst.setModel(new ListModelList(customer));
    
     // Custom renderer
     ListitemRenderer listitem = new ListitemRenderer() {
     @Override
     public void render(Listitem item, Object data) throws Exception {
     item.setValue(data);
     item.appendChild(new Listcell(((customerZK) data).getCompanyName()));
     item.appendChild(new Listcell(((customerZK) data).getFirstName() ));
     item.appendChild(new Listcell(((customerZK) data).getLastName() ));
     }
     };
     lst.setItemRenderer(listitem);
    
     } catch (Exception e) {
     System.out.println(e.getMessage());
     }
     }
    }
    
  5. Run the application
    The new table customerZK gets created. 

    table CUSTOMERZK

    The bowser shows an empty list

    Running application

  6. Insert data into the table
    Right-click on the table CUSTOMERZK | View data.. 

    View data (Netbeans)

     

  7. Refresh the application

    ZK listbox with data from EJB

Summary: In a few steps we created an Enterprise Bean with a session facade and a ZK web application that reads data from the DB (via the EJB) and displays it in a listbox. In the next tutorial we will add some functionality to modify and data from our ZK web application.

Complete Netbeans projects: http://sourceforge.net/projects/coffeeshop/files (ZK_EJB_0.1.zip and ZK_EJB_WEB_0.1.zip)

Advertisements

5 thoughts on “ZK with EJB3.1 running on Glassfish

  1. Pingback: ZK with EJB3.1 running on Glassfish (Part 2) « The JavaDude Weblog

  2. I got this exception when I tried to run the app:

    org.zkoss.lang.SystemException: org.xml.sax.SAXParseException: The processing instruction target matching “[xX][mM][lL]” is not allowed.

  3. Thank you so much… I’ve been going around in circles trying to get this functinality going for my project. ZK is beautiful and this post is more than helpful. Thanks again dude

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s