Sunday, 3 June 2012

MVC–CRUD Application with ZK 5 Data Binding

Here is the summary of what we have completed in this subject.
  1. We created new ZK Project, set up the hibernate and created Modal-View- Controller files to display all the record from the DB. Please click here to go that post.
  2. Next, we added filter feature such that if the user type any value, then the list will be filtered accordingly. Please click here to go that post
  3. Next, we’ve seen how to add more search parameters and filter the list. Please click here to go that post
All these are completed without using any data binding concept. Now let us see how we can use ZK 5 Data binding concepts and makes our job easy. If you are new ZK 5 Data binding, then first go here and learn basic stuffs.

Here is the Project structure so far for the files created.
To convert this example into ZK 5 Data binding, there is no need to change any MODAL Files such as Practice and PracticeDAO. Similarly there is no change in the Hibernate utility class and mapping file
All we need to create two files : one is ZUL File and its composer file
<?page title="Practice List" contentType="text/html;charset=UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="./plstWin" ?>

    <window title="Practice List" id="plstWin" border="normal"
        sclass="listingwindow" apply="myproject.UI.PracticeListController2">

            <button label="Add Practice" />
        <separator />
        <groupbox height="40px">
            <label value="Practice Name" />
            <space />
            <space />
            <textbox id="practicefilter" cols="50" />
            <button id="gobutton" label="Go" />
            <space spacing="20px" />
            <checkbox id="activechk" label="Show only active"
                checked="true" />
            <space spacing="20px" />
        <separator />
        <listbox id="PracticeList"
            <listhead sizable="true">
                <listheader label="Practice Name" sort="auto" />
                <listheader label="City" sort="auto" />
                <listheader label="State" sort="auto" />
                <listheader label="Zip Code" sort="auto" />
            <listitem self="@{each=pr1}">
                <listcell label="@{pr1.PracticeName}" />
                <listcell label="@{pr1.City}" />
                <listcell label="@{pr1.State}" />
                <listcell label="@{pr1.ZipCode}" />

package myproject.UI;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.InputEvent;
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;
import org.zkoss.zul.Checkbox;
import org.zkoss.zk.ui.event.Event;
import java.util.ArrayList;
import java.util.List;
import mydomain.*;

public class PracticeListController2 extends GenericForwardComposer {
    private static final long serialVersionUID = 1L;
    private Listbox PracticeList;
    private PracticeDAO practicedao;
    private Checkbox activechk;
    private List<practice> pLists;

    @SuppressWarnings({ "unchecked" })
    public void doAfterCompose(Component comp) throws Exception {
        practicedao = new PracticeDAO();
        pLists = practicedao.getAll();

    public void onChanging$practicefilter(InputEvent event) {
        pLists = practicedao.getFilteredPractices();
        PracticeList.setModel(new ListModelList(practicedao

    public void onCheck$activechk(Event event) {
        PracticeList.setModel(new ListModelList(practicedao


    public List<practice> getPLists() {
        return pLists;


1 comment:

  1. An useful and an important one information to enable displaying data in data grid’s cells.its also an important one.