-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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.
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.
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
You can find more examples here
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.
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
Maestro thanks for this :)
Asked: 2015-01-13 18:51:04 +0800
Seen: 49 times
Last updated: Jan 20 '15
ZK MVC CRUD With Spring 3 + JPA + Hibernate 4 Entity Manager
saving spread sheet data into database?
how to insert data to sql server database using jsp and zk?
Source files for the demo ZK in 10m- User Registration
how to communicating error message between dao and zul
how to set the selected item in a listbox related to a bandbox
Editable interface to add users