0

First ZK Test

asked 2015-01-13 18:51:04 +0800

Maestro2012 gravatar image Maestro2012
1

updated 2015-01-13 19:09:59 +0800

chillworld gravatar image chillworld flag of Belgium
5367 4 9
https://github.com/chillw...

Hello, I come by this post submit a problem that I encountered. Indeed, at the workplace, I was asked to develop an application using the zk framework. First I wanted to make a grip trying a CRUD on a small database named Client with one person table (id, name, first name). That's when I run the index.zul I exceptions that I do not understand. I put here my various java and zul files. help me please

Personne.java:

package essaibd.crud.bo;

public class Personne {
private String id;
private String nom;
private String prenom;

public Personne(){
}
public Personne (String id, String nom, String prenom){
    this.id = id;
    this.nom = nom;
    this.prenom = prenom;
}

public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getNom() {
    return nom;
}
public void setNom(String nom) {
    this.nom = nom;
}
public String getPrenom() {
    return prenom;
}
public void setPrenom(String prenom) {
    this.prenom = prenom;
}

}

PersonneDAO.java:

package essaibd.crud.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import essaibd.crud.bo.Personne;



public class PersonneDAO {
 private String url = "jdbc:mysql://localhost:3306/clientdb";

    private String user = "root";

    private String password = "passer";

public PersonneDAO (){
    try{
    Class.forName("com.mysql.jdbc.Driver");
    }
    catch (ClassNotFoundException e){
    e.printStackTrace();
    }
}
public List<Personne> getAllPersonne(){
    Statement stmt = null;
    Connection conn = null;
    List<Personne> maList = new ArrayList<Personne>();
    try{
         conn = DriverManager.getConnection("url, user, password");
         stmt = conn.createStatement();
         ResultSet rs = stmt.executeQuery("select * from personne");

         while (rs.next()){

            Personne p = new Personne();
            p.setId(rs.getString(1));
            p.setNom(rs.getString(2));
            p.setPrenom(rs.getString(3));
            maList.add(p);
        }
     } catch (SQLException e) {
            e.printStackTrace();
     }finally{
             try {
                    stmt.close();
             } catch (SQLException e) {
                  e.printStackTrace();
               }
               try {
                    conn.close();
                } catch (SQLException e) {
                  e.printStackTrace();
                }
    }
    return maList;
}
public boolean AddPerson (Personne p){
    Connection conn = null;
    Statement stmt = null;
    boolean result = false;
    try {
        // get connection
        conn = DriverManager.getConnection(url, user, password);
        stmt = conn.createStatement();
        if (stmt.executeUpdate("insert into personne(id,nom,prenom) " +
                "values ('" + p.getId() + "','" + p.getNom() +
                "','" + p.getPrenom() + "')") > 0);
        result = true;

    } catch (SQLException e) {
        e.printStackTrace();
    }finally{
        try {
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    return result;
}
public boolean UpdatePersonne (Personne p){
    Connection conn = null;
    Statement stmt = null;
    boolean result =false;
 try{   
     conn = DriverManager.getConnection("url, user, password");
     stmt = conn.createStatement();
     if (stmt.executeUpdate("update personne set nom = '" + p.getNom() + 
                "', prenom = " + p.getPrenom() + " where id = '" +p.getId() + "'") > 0);
    result = true;
    }catch (SQLException e){
        e.printStackTrace();
    }finally{
        try {
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try{
            conn.close();
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
 return result;
}
public boolean deletePersonne (Personne p){
    Connection conn = null;
    Statement stmt = null;
    boolean result =false;
     try{   
         conn = DriverManager.getConnection("url, user, password");
         stmt = conn.createStatement();
         if (stmt.executeUpdate("delete from peronne where id = '" + p.getId()+ "'") > 0);
         result = true;
     } catch (SQLException e) {
            e.printStackTrace();
     }finally {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
     return result;
}
}

PersonneControlleur.java:

package essaibd.crud.control;

import java.util.List;

import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Messagebox;

import essaibd.crud.bo.Personne;
import essaibd.crud.dao.PersonneDAO;

public class PersonneController extends GenericForwardComposer{

PersonneDAO persdao = new PersonneDAO();
Personne pers = new Personne();
Listbox box;

public Personne getPers() {
    return pers;
}
public void setPers(Personne pers) {
    this.pers = pers;
}

public List<Personne> getAllPersonne(){
    return persdao.getAllPersonne();
} 
public void onClick$add(){
    Personne newPers = new Personne(pers.getId(), pers.getNom(), pers.getPrenom());
    persdao.AddPerson(newPers);
    Messagebox.show("Personne ajoutée");
}
public void onClick$update(){
    if (box.getSelectedItem()!=null);
    persdao.UpdatePersonne((Personne) box.getSelectedItem().getValue());
}
public void onClick$delete() {      
    if (box.getSelectedItem() != null) {
        persdao.deletePersonne((Personne) box.getSelectedItem().getValue());
    }
}

}

index.zul:

<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>

<window id="win" title="Liste Personne" width="640px" border="normal" apply="essaibd.crud.control.PersonneController">
<listbox id="box" multiple="true" rows="5"   
selectedItem="@{win$composer.pers}">
    <listhead>
        <listheader label="Id"/>
        <listheader label="Nom" width="90px"/>
        <listheader label="Prenom" width="90px"/>
    </listhead>
    <listitem self="@{each='event'}" value="@{event}">
        <listcell label="@{event.id}" />
        <listcell label="@{event.nom}" />
        <listcell label="@{event.prenom}" />
    </listitem>
</listbox>
<groupbox>
    <caption label="Personne" />
    Id: <textbox id="num" cols="25" value="@{win$composer.pers.id}" />
    Nom: <textbox id="nom" cols="25" value="@{win$composer.pers.nom}" />
    Prenom: <textbox id="prenom" cols="25" value="@{win$composer.pers.prenom}" />
    <button id="add" label="Add" width="36px" height="24px"/>
    <button id="update" label="Update" width="46px" height="24px"/> 
    <button id="delete" label="Delete" width="46px" height="24px"/> 
</groupbox> 
</window>

and here is the error at runtime: Etat HTTP 500 - Expects java.util.Set, java.util.List, java.util.Map, Object[], Enum Class, GroupsModel, ListModel,or BindingListModel only. class essaibd.crud.control.PersonneController


type Rapport d'exception

message Expects java.util.Set, java.util.List, java.util.Map, Object[], Enum Class, GroupsModel, ListModel,or BindingListModel only. class essaibd.crud.control.PersonneController

description Le serveur a rencontré une erreur interne qui l'a empêché de satisfaire la requête.

exception 

org.zkoss.zk.ui.UiException: Expects java.util.Set, java.util.List, java.util.Map, Object[], Enum Class, GroupsModel, ListModel,or BindingListModel only. class essaibd.crud.control.PersonneController
org.zkoss.zkplus.databind.ListModelConverter.coerceToUi(ListModelConverter.java:67)
org.zkoss.zkplus.databind.Binding.myLoadAttribute(Binding.java:384)
org.zkoss.zkplus.databind.Binding.loadAttribute(Binding.java:337)
org.zkoss.zkplus.databind.DataBinder.loadAttrs(DataBinder.java:586)
org.zkoss.zkplus.databind.DataBinder.loadComponent0(DataBinder.java:543)
org.zkoss.zkplus.databind.DataBinder.loadAll(DataBinder.java:571)
org.zkoss.zkplus.databind.AnnotateDataBinderInit.doAfterCompose(AnnotateDataBinderInit.java:187)
org.zkoss.zk.ui.impl.Initiators$RealInits.doAfterCompose(Initiators.java:122)
org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:450)
org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:356)
org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:215)
org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:136)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

note La trace complète de la cause mère de cette erreur est disponible dans les fichiers journaux de Apache Tomcat/6.0.43.

delete flag offensive retag edit

6 Answers

Sort by » oldest newest most voted
0

answered 2015-01-13 20:25:38 +0800

chillworld gravatar image chillworld flag of Belgium
5367 4 9
https://github.com/chillw...

Your zul is really messed up.

First you get this error for this reason :

listbox id="box" multiple="true" rows="5" selectedItem="@{win$composer.pers}">

I don't see initialisation in the composer or in the zul for the model of the listbox. Change the listbox to the following code :

listbox id="box" multiple="true" rows="5" model="${win$composer.allPersonne}">

Yes I removed the selectedItem, but let's first try to run it without errors.
Second, you need to have a template in your listbox.

<template name="model">
    <listitem>
        <listcell label="${each.id}" />
        <listcell label="${each.nom}" />
        <listcell label="${each.prenom}" />
    </listitem>
</template>

Put the groupbox in comments and try this already out.
Normally you should have your listbox filled with data.

Greetz chill.

link publish delete flag offensive edit
0

answered 2015-01-14 00:51:19 +0800

cor3000 gravatar image cor3000
6280 2 7

updated 2015-01-14 02:03:58 +0800

Hi Maestro,

another problem with your code is, that it looks like you based it on an old example. The old databinding API you are trying to use has already been deprecated in ZK 7. Using this deprecated mechanism will make it more and more difficult to get support if problems arise.

Especially when creating a new application I strongly recommend basing it on the new DataBinding "ZK Bind". Our Tutorial "ZK Essentials" features 2 ways building a CRUD application using MVC and MVVM (with ZK Bind).

Regards,

Robert

link publish delete flag offensive edit
0

answered 2015-01-14 06:18:15 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

You can find more examples here

link publish delete flag offensive edit
0

answered 2015-01-16 13:33:02 +0800

Maestro2012 gravatar image Maestro2012
1

Thank you all for the answers you have given me, but when I entered your details, nor is there any end work. I took the project from a colleague who works (although with it we use the same approach) and I adapted to my needs to move because my boss puts pressure on me too. My other problem is I do not understand English months I ZK French and all documents are in English.

link publish delete flag offensive edit
0

answered 2015-01-19 17:02:43 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

All what i can do to help you is to point to Senthils example page or you take the complete base application from ZKBoost. It's not for beginners but well documented and structured.

best Stephan

link publish delete flag offensive edit
0

answered 2015-01-20 10:52:52 +0800

diullok gravatar image diullok
1
http://www.zabankowani.pl...

Maestro thanks for this :)

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2015-01-13 18:51:04 +0800

Seen: 49 times

Last updated: Jan 20 '15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More