ZK with EJB3.1 running on Glassfish (Part 2)

In part 1 we connected a web application to a database using an EJB for reading access, deployed on a Glassfish Server. In part 2 we do some modifications to create a true CRUD application by adding add, delete and update.


  • Netbeans 6.7 or later
  • Installed ZK plugin (link)
  • Glassfish V3 or later
  • Netbeans Project from Part 1 (link)


  1. EJB modifications
    are not required because for the basic CRUD functions we will use the provide abstract facade.

    package ejb;
    import java.util.List;
    import javax.persistence.EntityManager;
    public abstract class AbstractFacade<T> {
     private Class<T> entityClass;
     public AbstractFacade(Class<T> entityClass) {
     this.entityClass = entityClass;
     protected abstract EntityManager getEntityManager();
     public void create(T entity) {
     public void edit(T entity) {
     public void remove(T entity) {
     public T find(Object id) {
     return getEntityManager().find(entityClass, id);
     public List<T> findAll() {
     javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
     return getEntityManager().createQuery(cq).getResultList();
     public List<T> findRange(int[] range) {
     javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
     javax.persistence.Query q = getEntityManager().createQuery(cq);
     q.setMaxResults(range[1] - range[0]);
     return q.getResultList();
     public int count() {
     javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
     javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
     javax.persistence.Query q = getEntityManager().createQuery(cq);
     return ((Long) q.getSingleResult()).intValue();
  2. Modification of the zul file
    We add a button for delete and a grid with the 3 table columns, plus update and create buttons.

    <?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" />
     <button id="btnDelete" label="Delete record"/>
     <separator bar="true"/>
     <grid width="40%">
     <column label=""/>
     <column label=""/>
     <label value="Company:" />
     <textbox id="txtCompany" value=""/>
     <label value="First:" />
     <textbox id="txtFirst" value=""/>
     <label value="Last:" />
     <textbox id="txtLast" value=""/>
     <button id="btnUpdate" label="Update record"/>
     <button id="btnAdd" label="Add record"/>
  3. Modification to the controller class
    We need to add variables for the additional components, create a separate instance of the model to update the listbox without re-reading the data from the DB when changes are applied.

    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.event.Event;
    import org.zkoss.zk.ui.util.GenericForwardComposer;
    import org.zkoss.zul.Button;
    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;
    import org.zkoss.zul.Textbox;
    public class indexController extends GenericForwardComposer {
     Context context;
     customerFacade facade;
     List<customerZK> customer = null;
     ListModelList model;
     private Listbox lst;
     private Textbox txtLast;
     private Textbox txtFirst;
     private Textbox txtCompany;
     public void doAfterCompose(Component comp) throws Exception {
     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();
     // Set listmodel for the ZK list component
     model = new ListModelList(customer);
     // Custom renderer
     ListitemRenderer listitem = new ListitemRenderer() {
     public void render(Listitem item, Object data) throws Exception {
     item.appendChild(new Listcell(((customerZK) data).getCompanyName()));
     item.appendChild(new Listcell(((customerZK) data).getFirstName()));
     item.appendChild(new Listcell(((customerZK) data).getLastName()));
    } catch (Exception e) {
     public void onSelect$lst(Event evt) throws InterruptedException {
     // read values from selected record
     txtLast.setValue(((customerZK) lst.getSelectedItem().getValue()).getLastName());
     txtFirst.setValue(((customerZK) lst.getSelectedItem().getValue()).getFirstName());
     txtCompany.setValue(((customerZK) lst.getSelectedItem().getValue()).getCompanyName());
     public void onClick$btnDelete(Event evt) throws InterruptedException {
     System.out.println("Deleting ID: " + ((customerZK) lst.getSelectedItem().getValue()).getId());
     // persist
     facade.remove((customerZK) lst.getSelectedItem().getValue());
     // update listmodel
     model.remove((customerZK) lst.getSelectedItem().getValue());
     public void onClick$btnUpdate(Event evt) throws InterruptedException {
     customerZK modCustomer = new customerZK();
     // read values from textbox
     modCustomer.setId(((customerZK) lst.getSelectedItem().getValue()).getId());
     // persist
     // update listmodel
     model.set(model.indexOf((customerZK) lst.getSelectedItem().getValue()) , modCustomer);
     public void onClick$btnAdd(Event evt) throws InterruptedException {
     customerZK modCustomer = new customerZK();
     // persist
     // update listmodel
  4. Run the application

    Running ZK application


3 thoughts on “ZK with EJB3.1 running on Glassfish (Part 2)

  1. Hi dude,

    why do you prefer to lookup the facade instead of injecting it? Like:
    @EJB customerFacade facade;

    about your first final remarks, a smarter way could be to use the zk Binder and the @ annotations in the zul?

  2. Hello, both your tutorials helped me a lot to bind ZK and EJB. thanks a lot
    However i have two entities, with master detail relationship. I save the a detail record, it gets saved (i check the db) but the master entity doesnt seem to understand it.

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 )

Connecting to %s