Text Box on change and on Changing Event

ZK Demo site shows the example using ZUL File how to handle onChanging event. Let us see how we can handle in java using MVC  GenericForwardComposer composer

Zul File

<?page title="On Change Event" contentType="text/html;charset=UTF-8"?>
<zk>
    <window title="On Change Event" border="normal" apply="mypack.OnchangeTxt">
        <grid width="100%">
            <rows>
                <row>
                    onChanging textbox:
                    <textbox id="t1" />
                </row>
                <row>
                    Instant copy:
                    <textbox id="copy" readonly="true" />
                </row>
            </rows>
        </grid>
 
    </window>
</zk>


Controller



package mypack;
 
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.InputEvent;
import org.zkoss.zul.Textbox;
 
public class OnchangeTxt extends GenericForwardComposer {
 
    private Textbox copy;
 
    public void onChanging$t1(InputEvent event) {
        copy.setValue(event.getValue());
 
    }
}

Hibernate Criteria Queries

The Hibernate Session interface provides createCriteria() method which can be used to create a Criteria object that returns instances of the persistence object's class when your application executes a criteria query

Environment

  1. Eclipse 3.7 Indigo IDE
  2. Hibernate 4.1.1
  3. JavaSE 1.6
  4. MySQL 5.1

Step 1:

Create the following table in mysql
image
Insert the records as follows
image

Step 3:

In the Eclipse, Select File –> New –> Java Project and give Project Name as HibernateCriteria
Click Next –> Go to Libraries Tab-> Click External Jar Files and include the following jar files
  • antlr-2.7.7.jar
  • dom4j-1.6.1.jar
  • hibernate-commons-annotations-4.0.1.Final.jar
  • hibernate-core-4.1.1.Final.jar
  • hibernate-entitymanager-4.1.1.Final.jar
  • hibernate-jpa-2.0-api-1.0.1.Final.jar
  • javassist-3.15.0-GA.jar
  • jboss-logging-3.1.0.GA.jar
  • jboss-transaction-api_1.1_spec-1.0.0.Final.jar
  • mysql-connector-java-5.1.18-bin.jar

Step 4:

Create the Employee POJO Class as follows
image
  1:  package mypack;
 
   3:  import javax.persistence.Entity;
   4:  import javax.persistence.GeneratedValue;
   5:  import javax.persistence.Id;
   6:  import javax.persistence.Table;
   7:  import javax.persistence.Column;
   8:   
   9:   
  10:  @Entity
  11:  @Table(name="employee")
  12:  public class Employee {
  13:      private int id;
  14:      private String firstName;
  15:      private String lastName;
  16:      private int salary;
  17:      private int IsActive;
  18:   
  19:      public Employee() {
  20:      }
  21:   
  22:      public Employee(String fname, String lname, int salary) {
  23:          this.firstName = fname;
  24:          this.lastName = lname;
  25:          this.salary = salary;
  26:      }
  27:   
  28:      @Id
  29:      @GeneratedValue
  30:      @Column(name="ID")
  31:      public int getId() {
  32:          return id;
  33:      }
  34:   
  35:      public void setId(int id) {
  36:          this.id = id;
  37:      }
  38:   
  39:      @Column(name="firstName")
  40:      public String getFirstName() {
  41:          return firstName;
  42:      }
  43:   
  44:      public void setFirstName(String first_name) {
  45:          this.firstName = first_name;
  46:      }
  47:   
  48:      @Column(name="lastName")
  49:      public String getLastName() {
  50:          return lastName;
  51:      }
  52:   
  53:      public void setLastName(String last_name) {
  54:          this.lastName = last_name;
  55:      }
  56:   
  57:      @Column(name="salary")
  58:      public int getSalary() {
  59:          return salary;
  60:      }
  61:   
  62:      public void setSalary(int salary) {
  63:          this.salary = salary;
  64:      }
  65:      
  66:      @Column(name="IsActive")
  67:      public int getIsActive() {
  68:          return IsActive;
  69:      }
  70:   
  71:      public void setIsActive(int IsActive) {
  72:          this.IsActive = IsActive;
  73:      }
  74:   
  75:  }
 

Step 5:


Let us create Hibernate Configuration file hibernate.cfg.xml




   1: <?xml version="1.0" encoding="utf-8"?>
   2: <!DOCTYPE hibernate-configuration PUBLIC
   3:  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
   4:  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
   5: <hibernate-configuration>
   6:     <session-factory>
   7:         <!-- Database connection settings -->
   8:         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
   9:         <property name="connection.url">jdbc:mysql://localhost/sampledb</property>
  10:         <property name="connection.username">root</property>
  11:         <property name="connection.password">123</property>
  12:  
  13:         <!-- JDBC connection pool (use the built-in) -->
  14:         <property name="connection.pool_size">1</property>
  15:  
  16:         <!-- SQL dialect -->
  17:         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  18:  
  19:         <!-- Enable Hibernate's automatic session context management -->
  20:         <property name="current_session_context_class">thread</property>
  21:  
  22:         <!-- Disable the second-level cache -->
  23:         <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
  24:  
  25:         <!-- Echo all executed SQL to stdout -->
  26:         <property name="show_sql">true</property>
  27:  
  28:         <!-- Drop and re-create the database schema on startup -->
  29:         <property name="hbm2ddl.auto">update</property>
  30:  
  31:         <!-- Mapping Classes -->
  32:         <mapping class="mypack.Employee" />
  33:  
  34:     </session-factory>
  35: </hibernate-configuration>

Take care to change the DB Name, User name and Password according to your setup


Step 6:




Next we will create our Test.java in which we are going to test different Hibernate Criteria Queries

image

Here is the Code





   1: package mypack;
   2:  
   3: import java.util.List;
   4: import org.hibernate.Criteria;
   5: import org.hibernate.Query;
   6: import org.hibernate.Session;
   7: import org.hibernate.SessionFactory;
   8: import org.hibernate.Transaction;
   9: import org.hibernate.cfg.Configuration;
  10: import org.hibernate.service.ServiceRegistry;
  11: import org.hibernate.service.ServiceRegistryBuilder;
  12: import org.hibernate.HibernateException;
  13:  
  14: public class Test {
  15:  
  16:     private static SessionFactory factory;
  17:     private static ServiceRegistry serviceRegistry;
  18:  
  19:     public static void main(String[] args) {
  20:         getAllRecords();
  21:     }
  22:  
  23:     public static void getAllRecords() {
  24:         try {
  25:             Configuration configuration = new Configuration();
  26:             configuration.configure();
  27:             serviceRegistry = new ServiceRegistryBuilder().applySettings(
  28:                     configuration.getProperties()).buildServiceRegistry();
  29:             factory = configuration.buildSessionFactory(serviceRegistry);
  30:         } catch (Throwable ex) {
  31:             System.err.println("Failed to create sessionFactory object." + ex);
  32:             throw new ExceptionInInitializerError(ex);
  33:         }
  34:         System.out.println("**Example : Hibernate  SessionFactory**");
  35:         System.out.println("----------------------------------------");
  36:         Session session = factory.openSession();
  37:         Transaction tx = null;
  38:         try {
  39:             tx = session.beginTransaction();
  40:             Criteria cr = session.createCriteria(Employee.class);
  41:             List results = cr.list();
  42:             for (int i = 0; i < results.size(); i++) {
  43:                 Employee emp = (Employee) results.get(i);
  44:                 System.out.println(emp.getFirstName());
  45:             }
  46:             tx.commit();
  47:         } catch (HibernateException e) {
  48:             if (tx != null)
  49:                 tx.rollback();
  50:             e.printStackTrace();
  51:         } finally {
  52:             session.close();
  53:         }
  54:     }
  55: }


Step 7:




Now let us run the Test.Java as Java Application

In the console,  did you see the following errorSad smile 

----------------------------------------
Hibernate: select this_.ID as ID0_0_, this_.firstName as firstName0_0_, this_.IsActive as IsActive0_0_, this_.lastName as lastName0_0_, this_.salary as salary0_0_ from employee this_
org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of mypack.Employee.isActive





If one declares these int,bit,char as Primitive data types in POJO classes.. then we cannot do getter/setter NULL value into this attributes.. but on you these values will allow NULL to store.. so to avoid this conflicts its always suggestible to use WRAPPER classes itself not Primitive types.
So, We would need to initialize these variable with WRAPPER classes only
like int - "Integer"
bit - "Boolean"
char - "Character"




So we will change our POJO Class, because we have two columns where null value are stored and that has been declared as int. So let us change to integer.

Here is the code





   1: package mypack;
   2: import javax.persistence.Entity;
   3: import javax.persistence.GeneratedValue;
   4: import javax.persistence.Id;
   5: import javax.persistence.Table;
   6: import javax.persistence.Column;
   7:  
   8: @Entity
   9: @Table(name="employee")
  10: public class Employee {
  11:     private int id;
  12:     private String firstName;
  13:     private String lastName;
  14:     private Integer salary;
  15:     private Integer IsActive;
  16:  
  17:     public Employee() {
  18:     }
  19:  
  20:     public Employee(String fname, String lname, int salary) {
  21:         this.firstName = fname;
  22:         this.lastName = lname;
  23:         this.salary = salary;
  24:     }
  25:     @Id
  26:     @GeneratedValue
  27:     @Column(name="ID")
  28:     public int getId() {
  29:         return id;
  30:     }
  31:  
  32:     public void setId(int id) {
  33:         this.id = id;
  34:     }
  35:  
  36:     @Column(name="firstName")
  37:     public String getFirstName() {
  38:         return firstName;
  39:     }
  40:  
  41:     public void setFirstName(String first_name) {
  42:         this.firstName = first_name;
  43:     }
  44:  
  45:     @Column(name="lastName")
  46:     public String getLastName() {
  47:         return lastName;
  48:     }
  49:  
  50:     public void setLastName(String last_name) {
  51:         this.lastName = last_name;
  52:     }
  53:  
  54:     @Column(name="salary")
  55:     public Integer getSalary() {
  56:         return salary;
  57:     }
  58:  
  59:     public void setSalary(Integer salary) {
  60:         this.salary = salary;
  61:     }
  62:     
  63:     @Column(name="IsActive")
  64:     public Integer getIsActive() {
  65:         return IsActive;
  66:     }
  67:  
  68:     public void setIsActive(Integer IsActive) {
  69:         this.IsActive = IsActive;
  70:     }
  71: }

Now let us again run the Test.Java as Java Application

Wow!!!! Smile, Output as follows
**Example : Hibernate  SessionFactory**
----------------------------------------
Hibernate: select this_.ID as ID0_0_, this_.firstName as firstName0_0_, this_.IsActive as IsActive0_0_, this_.lastName as lastName0_0_, this_.salary as salary0_0_ from employee this_
John
Robert
Richard
Anderson
Lee
Nelson




Step 8:




Now let us see how we can retrieve all the Active Employee.

image


Here Anderson is active. And also let us consider if that column contains null value, then we will treat that also Active Employees.Here is the Criteria





   1: Criteria cr = session.createCriteria(Employee.class);
   2:             Criterion cr1 = Restrictions.eq("isActive", 1);
   3:             Criterion cr2 = Restrictions.isNull("isActive");
   4:             // To get records matching with OR condistions
   5:             LogicalExpression orExp = Restrictions.or(cr1, cr2);
   6:             cr.add( orExp );
   7:             List results = cr.list();
Assume that we need only Inactive Employee. So as per the records, there are two employee (b’0’)

Criteria cr = session.createCriteria(Employee.class);
        cr.add(Restrictions.eq("isActive", 0));
        List results = cr.list();

MVC CRUD Application


So what we are going to do with MVC CRUD Application. Well, any typical master screen will have one listing page where all the records are displayed and will also have the CRUD operation such as Add, Edit, Update and Delete.

Hibernate Query uniqueResult Method

 

After completing my Hibernate Post, I want to know what will happen if there are more than one row expected when we use uniqueResult method.

So in the same example, I put one more method called getPatientByLastName, where we can pass any last name and print the other details of the patient.

Here is the complete code for Test.Java as per Part 4

package mypack;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.HibernateException;

public class Test {

    private static SessionFactory factory;
    private static ServiceRegistry serviceRegistry;

    public static void main(String[] args) {
        /*
        createRecords();
        getAllRecords();
         getPatientByID(1);
        getPatientByID(13);
        */
        getPatientByLastName("Smith");
       
    }

    public static void createRecords() {

        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(
                    configuration.getProperties()).buildServiceRegistry();
            factory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex);
        }
        System.out.println("**Example : Hibernate 4 SessionFactory**");
        System.out.println("----------------------------------------");

        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            patient pt = new patient();
            pt.setFirstName("John12");
            pt.setLastName("Smith23");
            session.saveOrUpdate(pt);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
   
    public static void getAllRecords() {

        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(
                    configuration.getProperties()).buildServiceRegistry();
            factory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex);
        }
        System.out.println("**Example : Hibernate  SessionFactory**");
        System.out.println("----------------------------------------");
       
        List allpatients;
       
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Query q1 = session.createQuery("from patient");
            allpatients = q1.list();
            for (int i =0; i < allpatients.size(); i++)
            {
                patient pt = (patient) allpatients.get(i);
                System.out.println(pt.getLastName());
            }
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }

   
    public static void getPatientByID(int anyvalue) {

        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(
                    configuration.getProperties()).buildServiceRegistry();
            factory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex);
        }
        System.out.println("**Example : Hibernate  SessionFactory**");
        System.out.println("----------------------------------------");
       
       
       
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Query q1 = session.createQuery("from patient where id =:idvalue");
            q1.setInteger("idvalue", anyvalue);
            Object queryResult = q1.uniqueResult();
            patient p1 = (patient)queryResult;
            tx.commit();
            System.out.print(p1.getLastName());
        } catch (HibernateException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }

   
    public static void getPatientByLastName(String anyvalue) {

        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(
                    configuration.getProperties()).buildServiceRegistry();
            factory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex);
        }
        System.out.println("**Example : Hibernate  SessionFactory**");
        System.out.println("----------------------------------------");
       
       
       
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Query q1 = session.createQuery("from patient where lastname =:varLastName");
            q1.setString("varLastName",anyvalue);
            Object queryResult = q1.uniqueResult();
            patient p1 = (patient)queryResult;
            tx.commit();
            System.out.print(p1.getLastName());
        } catch (HibernateException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }

}

If we run this, we will get the following error without any problem. Thank god, at least hibernate throwing exception if something is wrong.

**Example : Hibernate  SessionFactory**
----------------------------------------
Hibernate: select patient0_.ID as ID0_, patient0_.firstname as firstname0_, patient0_.lastname as lastname0_ from patient patient0_ where lastname=?
org.hibernate.NonUniqueResultException: query did not return a unique result: 2
    at org.hibernate.internal.AbstractQueryImpl.uniqueElement(AbstractQueryImpl.java:899)
    at org.hibernate.internal.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:890)
    at mypack.Test.getPatientByLastName(Test.java:162)
    at mypack.Test.main(Test.java:26)

Hibernate retrieve the first record always when we try retrieve all records

 

Today I come across a very strange behavior with hibernate while I am trying to retrieve all the records.

Here is that

1. Go to my Hibernate Introduction Part 4 Post

2. In the Patient.Java bean class, you can see the following lines

@Id
@GeneratedValue
@Column(name="ID")
public Integer getId() {
    return id;
}

Assume that we misspelled the Column name id as RowID (this may happen when we cut and paste code from different project/examples)

Now it will look like

@Id
@GeneratedValue
@Column(name="RowID")
public Integer getId() {
    return id;
}

 

Now go to Test.Java and comment all the methods except getallRecords()

Now run the application

You can observer that first record is repeated up to total records in the table.

 

Hibernate Introduction Part 4

 

This article is a continuation of our previous article.

In the previous post, we have seen how to retrieve all the records in the database. But most of time, this is not the case.  Sometime we need to retrieve particular record by passing the primary key.

Say for example, in SQL

Select * from User where UserID=’John’

This can be achieved in HQL using Variable injection option

And also , When we expect multiple rows, then we know what we can use Query’s List method() as we seen in the previous example. On the other hand, When we expect a single record, then we can use Query’s  uniqueResult()

 

Here is the full code for the Method to retrieve the unique record

 

public static void getPatientByID(int anyvalue) {

    try {
        Configuration configuration = new Configuration();
        configuration.configure();
        serviceRegistry = new ServiceRegistryBuilder().applySettings(
                configuration.getProperties()).buildServiceRegistry();
        factory = configuration.buildSessionFactory(serviceRegistry);
    } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex);
    }
    System.out.println("**Example : Hibernate  SessionFactory**");
    System.out.println("----------------------------------------");
   
    Session session = factory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        Query q1 = session.createQuery("from patient where id =:idvalue");
        q1.setInteger("idvalue", anyvalue);
        Object queryResult = q1.uniqueResult();
        patient p1 = (patient)queryResult;
        tx.commit();
        System.out.print(p1.getLastName());
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }
}

 

Here is the updated test.java code

package mypack;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.HibernateException;

public class Test {

    private static SessionFactory factory;
    private static ServiceRegistry serviceRegistry;

    public static void main(String[] args) {
        createRecords();
        getAllRecords();
        getPatientByID(1);
        getPatientByID(13);
       
    }

    public static void createRecords() {

        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(
                    configuration.getProperties()).buildServiceRegistry();
            factory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex);
        }
        System.out.println("**Example : Hibernate 4 SessionFactory**");
        System.out.println("----------------------------------------");

        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            patient pt = new patient();
            pt.setFirstName("John12");
            pt.setLastName("Smith23");
            session.saveOrUpdate(pt);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
   
    public static void getAllRecords() {

        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(
                    configuration.getProperties()).buildServiceRegistry();
            factory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex);
        }
        System.out.println("**Example : Hibernate  SessionFactory**");
        System.out.println("----------------------------------------");
       
        List allpatients;
       
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Query q1 = session.createQuery("from patient");
            allpatients = q1.list();
            for (int i =0; i < allpatients.size(); i++)
            {
                patient pt = (patient) allpatients.get(i);
                System.out.println(pt.getLastName());
            }
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }

   
    public static void getPatientByID(int anyvalue) {

        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(
                    configuration.getProperties()).buildServiceRegistry();
            factory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex);
        }
        System.out.println("**Example : Hibernate  SessionFactory**");
        System.out.println("----------------------------------------");
       
       
       
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Query q1 = session.createQuery("from patient where id =:idvalue");
            q1.setInteger("idvalue", anyvalue);
            Object queryResult = q1.uniqueResult();
            patient p1 = (patient)queryResult;
            tx.commit();
            System.out.print(p1.getLastName());
        } catch (HibernateException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }

}

ZK Data Binding Part 1

 

Before seeing the ZK Data binding concept , first let us learn the for each attribute in zk

The following zul file is simple which has the list items

<?page title="Example1" contentType="text/html;charset=UTF-8"?>
<zk>
    <window title="Example1" border="normal">
        Example for "for each"
        <listbox>
            <listitem label="Apple" />
            <listitem label="Orange" />
            <listitem label="Strawberry" />
        </listbox>
    </window>
</zk>

image

The output can be expected if you use for each attribute as follows

 

<?page title="Example2" contentType="text/html;charset=UTF-8"?>
<zk>
    <window title="Example2" border="normal">
        Example for "for each"
        <listbox>
            <listitem label="${each}"
                forEach="Apple, Orange, Strawberry" />
        </listbox>
    </window>
</zk>

 

The forEach attribute is used to specify a collection of object such that the XML element it belongs will be evaluated repeatedly for each object of the collection.

Simple Sightseeing Application Part 2



In the part 1, we have seen how to work on sight seeing application locally by having some
static values. In this part, we will see how we can connect to the database using hibernate and
retrive the values from the table

Window CSS

ZK Simple Sightseeing Application Part 1


After reading the following article,
http://books.zkoss.org/wiki/ZK_Getting_Started/Creating_a_Simple_Sightseeing_Application

I decided to connect with the DB and to make database driven application.

Button CSS

 
 
All
ZK Button CSS
 

Group Box with Collapse and Expand Button in the Right

Group Box with Collapse and Expand Button in the Right

Tuning Eclipse Performance and Avoiding OutOfMemoryExceptions



If you are using 4 GB Ram, the following will help you to increase the speed of the eclipse

Goto the Folder where eclipse.exe is located.

Open the eclipse.ini


-startup
plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.100.v20110502
-product
org.eclipse.epp.package.jee.product
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
--launcher.defaultAction
openFile
-vm
C:/Program Files/Java/jdk1.6.0_21/bin/javaw
-vmargs
-Xmn128m
-Xms1024m
-Xmx1024m
-Xss2m
-XX:PermSize=128m
-XX:MaxPermSize=128m
-XX:+UseParallelGC


For other than 4 GB, read the following article



http://madhucm.wordpress.com/2007/12/19/tuning-eclipse-performance-and-avoiding-outofmemoryexceptions/


Listitem Mold Paging - Add components in the pagination bar


<zk>
 <zscript>
  Object[] o = new Object[50];
 </zscript>
 <toolbar>
  <hlayout sclass="z-valign-middle">
   <toolbarbutton label="other button 1"></toolbarbutton>
   <paging id="pagenavi" pageSize="10"></paging>
   <toolbarbutton label="other button 2"></toolbarbutton>
  </hlayout>
 </toolbar>
 <listbox id="listbox" mold="paging" paginal="${pagenavi}">
  <listhead>
   <listheader label="name"></listheader>
  </listhead>
  <listitem forEach="${o}">
   <listcell label="item ${forEachStatus.index}" />
  </listitem>
 </listbox>
</zk>

For more information, please refer the following thread
http://www.zkoss.org/forum/listComment/19535-Listbox-mold-paging

ZK Calling Another ZUL File

Method 1


<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="new page title" border="normal">
New Content Here!
<button label="click me" href="../main/Practice.zul"/>
</window>
</zk>


Method 2


<zk>
  <window id="mainwin" border="normal" title="hello" xmlns:c="client">
 
  <button label="Practice">
          <attribute name="onClick">
            Executions.createComponents("../main/Practice.zul",mainwin,null);
          </attribute>
    </button>
  </window>
</z

ZK Installation Guide


Part 1.
Explains how to setup java, maven, Tomcat and Eclipse IDE. You can download the document here.

Part 2
Explains how to setup ZK Studio and ZK CE Version. You can download the document here.

Part 3
Explains how to download and setup ZK Sample 2 application locally. You can download the document here.


 

Eclipse Tips and Tricks

1. External Browser

Whenever you run the application, you can see the output using internal browser. By normally i will not prefer that method. So you can change the settings, to open the output using any external browser

Here is the steps

1. Click Window -> Preferences-> General-> Web Browser
    Change to External web Browser. Choose from the list. Here my choice is fire fox