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
- Create a new Java EE project, type EJB module (ZK_EJB).
- 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) - Add fields to entity class
private String lastName; private String firstName; private String companyName;
Right-click into the code | Insert Code | Getter and Setter..
- Create a session facade
- Build the project
Creates a ZK_EJB.jar in the dist folder.
Create the ZK application
- Create a new web application, type ZKx.y.z JavaEE6 Application (ZK_EJB_WEB)
- 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) - 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>
- 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()); } } }
- Run the application
The new table customerZK gets created.The bowser shows an empty list
- Insert data into the table
Right-click on the table CUSTOMERZK | View data.. - Refresh the application
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)
Pingback: ZK with EJB3.1 running on Glassfish (Part 2) « The JavaDude Weblog
Names of classes starting with a small letter?
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.
great post….thks dude…:D
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