-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi I am trying to populate country name from my database into the combobox and but still unsuccessful. Could you please tell me where is the mistake that I have ?
The codes of different files are given below-
in Customer. zul file
inside grid:
<cell colspan="1"> <combobox id="country" cols="25" value="@{win$composer.current.country}" apply="com.integms.country.CountryCombo" /> </cell>
CountryCombo.java file
package com.integms.country; import java.util.ArrayList; import java.util.List; import org.zkoss.zk.ui.util.GenericForwardComposer; import org.zkoss.zul.Combobox; import org.zkoss.zul.ListModelList; import org.zkoss.zul.ListModels; public class CountryCombo extends GenericForwardComposer { /** * */ private static final long serialVersionUID = 1L; Combobox country; public void afterCompose() { //super.afterCompose(); System.out.println("CountryCombo1"); country.setModel(ListModels.toListSubModel(new ListModelList(getAllItems()))); } List <Country> getAllItems() { //return all items List<Country> allCountry = new ArrayList<Country>(); CountryDAO cDAO=new CountryDAO(); allCountry=cDAO.findAll(); System.out.println("CountryCombo2"); return allCountry; } }
CountryDAO.java
package com.integms.country; /** * Country DAO. * * @author Mortoza */ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.integms.lib.DataSource; public class CountryDAO { public List<Country> findAll() { DataSource ds = new DataSource(); List<Country> allCountry = new ArrayList<Country>(); try { // get connection Statement stmt = ds.getStatement(); ResultSet rs = stmt.executeQuery("select id, country from country"); // fetch all events from database Country cust; while (rs.next()) { cust = new Country(); cust.setId(rs.getString(1)); cust.setCountry(rs.getString(2)); allCountry.add(cust); } } catch (SQLException e) { e.printStackTrace(); } finally { ds.close(); } return allCountry; } public List<Country> findAllCountry() { DataSource ds = new DataSource(); List<Country> allCountry = new ArrayList<Country>(); try { // get connection Statement stmt = ds.getStatement(); ResultSet rs = stmt.executeQuery("select country from country"); // fetch all events from database Country cust; while (rs.next()) { cust = new Country(); cust.setCountry(rs.getString(2)); allCountry.add(cust); } } catch (SQLException e) { e.printStackTrace(); } finally { ds.close(); } return allCountry; } public boolean delete(Country cust) { return execute("delete from country where id = '" + cust.getId() + "'"); } public boolean insert(Country cust) { return execute("insert into country (id, country) " + "values ('"+cust.getId()+"' , '"+cust.getCountry()+"'"); } public boolean update(Country cust) { return execute("update country set country = '"+cust.getCountry()+"' where id = '" + cust.getId() + "'"); } private boolean execute(String sql) { DataSource ds = new DataSource(); try { Statement stmt = ds.getStatement(); stmt.execute(sql); if (stmt != null) { stmt.close(); } return true; } catch (SQLException e) { return false; } finally { ds.close(); } } }
Hi samchuang
Thank you your reply. I have gone through the referred documents but I am wondering I must have done some mistake somewhere! Could you please help me where did you make the mistake, my country combo box is not displaying anything, now I am getting an exception.
Please help. regards, Mortoza
CountryController.jave
public class CountryController extends GenericForwardComposer { private static final long serialVersionUID = -9145887024839938515L; private Country current = new Country(); private CountryDAO cDao = new CountryDAO(); private Textbox id; private Combobox country; public void afterCompose() { // super.afterCompose(); country.setModel(ListModels.toListSubModel(new ListModelList(getAllItems()))); } List<Country> getAllItems() { CountryDAO cDAO=new CountryDAO(); return cDAO.findAll(); }
CountryDAO.java
public class CountryDAO { public List<Country> findAll() { DataSource ds = new DataSource(); List<Country> allCountry = new ArrayList<Country>(); try { // get connection Statement stmt = ds.getStatement(); ResultSet rs = stmt.executeQuery("select id, country from country"); // fetch all events from database Country cust; while (rs.next()) { cust = new Country(); cust.setId(rs.getString(1)); cust.setCountry(rs.getString(2)); allCountry.add(cust); } } catch (SQLException e) { e.printStackTrace(); } finally { ds.close(); } return allCountry; }
ZUL file
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?> <?page title="Country" contentType="text/html;charset=UTF-8"?> <zk> <window closable="true" minimizable="true" id="win" title="Customer profile" width="800px" height="600px" border="normal" apply="com.integms.country.CountryController"> New Content Here! <combobox id="country" cols="25" model="@{country}" value="@{win$composer.current.country}" /> </window> </zk>
I am now getting the following error. Please help.
type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. 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 org.zkoss.zul.Combobox org.zkoss.zkplus.databind.ListModelConverter.coerceToUi(ListModelConverter.java:63) 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:591) org.zkoss.zkplus.databind.DataBinder.loadComponent0(DataBinder.java:546) org.zkoss.zkplus.databind.DataBinder.loadAll(DataBinder.java:575) org.zkoss.zkplus.databind.AnnotateDataBinderInit.doAfterCompose(AnnotateDataBinderInit.java:189) org.zkoss.zk.ui.impl.RealInits.doAfterCompose(Initiators.java:107) org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:395) org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:309) org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:220) org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:140) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
Thanks in advance. Mortoza
Hi Samchuang,
Thanks. I have done some changes, now I can see some texts in the combo box loaded like
"com.integms.country.Country@1187d2f"
com.integms.country.Country@1721e22
com.integms.country.Country@c2c65b
any additional tips?
regards
Mortoza
It invokes toString() on your Country object. Two choices:
1) you implement toString()
2) you have another property (e.g. getName() or getLabel()) and specify your comboitem explcitly (example with property name):
<combobox id="country" cols="25" model="@{country}" value="@{win$composer.current.country}"> <comboitem self="@{each=_country}" value="@{_country}" label="@{_country.name}" /> </combobox>
Hi Matze2
Thank you so much for your help. Unfortunately it's still not working. I realised table name and field name is same "country" and that might be confusing to me. Therefore, I renamed the field name country to countryname. I provide below all the code again for your quick review and help please.
customer.zul
<combobox id="country" cols="25" model="@{country}" value="@{win$composer.current.country}" apply="com.integms.country.CountryCombo"> <comboitem self="@{each=_country}" value="@{_country}" label="@{_country.countryname}" /> </combobox>
Country.java
public class Country { private String id; private String countryname; public Country() { } public Country(String id, String countryname) { this.id = id; this.countryname = countryname; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCountryname() { return countryname; } public void setCountryname(String countryname) { this.countryname = countryname; } }
CountryDAO.java
import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.integms.lib.DataSource; public class CountryDAO { public List<Country> findAll() { DataSource ds = new DataSource(); List<Country> allCountry = new ArrayList<Country>(); try { // get connection Statement stmt = ds.getStatement(); ResultSet rs = stmt.executeQuery("select id, countryname from country"); // fetch all events from database Country c; while (rs.next()) { c = new Country(); c.setId(rs.getString(1)); c.setCountryname(rs.getString(2)); allCountry.add(c); } } catch (SQLException e) { e.printStackTrace(); } finally { ds.close(); } return allCountry; } public List<Country> findAllCountry() { DataSource ds = new DataSource(); List<Country> allCountry = new ArrayList<Country>(); try { // get connection Statement stmt = ds.getStatement(); ResultSet rs = stmt.executeQuery("select countryname from country"); // fetch all events from database Country c; while (rs.next()) { c = new Country(); c.setCountryname(rs.getString(1)); allCountry.add(c); } } catch (SQLException e) { e.printStackTrace(); } finally { ds.close(); } return allCountry; }
Exception when I run
org.zkoss.zk.ui.UiException: Expects java.util.Set, java.util.List, java.util.Map, Object[], Enum Class, GroupsModel, ListModel,or BindingListModel only. class org.zkoss.zul.Combobox at org.zkoss.zkplus.databind.ListModelConverter.coerceToUi(ListModelConverter.java:63) at org.zkoss.zkplus.databind.Binding.myLoadAttribute(Binding.java:384) at org.zkoss.zkplus.databind.Binding.loadAttribute(Binding.java:337) at org.zkoss.zkplus.databind.DataBinder.loadAttrs(DataBinder.java:591) at org.zkoss.zkplus.databind.DataBinder.loadComponent0(DataBinder.java:546) at org.zkoss.zkplus.databind.DataBinder.loadAll(DataBinder.java:575) at org.zkoss.zkplus.databind.AnnotateDataBinderInit.doAfterCompose(AnnotateDataBinderInit.java:189) at org.zkoss.zk.ui.impl.RealInits.doAfterCompose(Initiators.java:107) at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:395) at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:309) at org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:220) at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:140) at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
Thanks in advance for helping again.
regards
Hi Matze2
This may be mentioned here that the zul code lines are from customer.zul file and composer if customer composer where composer is customer composer. do you also suggest to apply countryController? I also tried that but didnt work.
Asked: 2012-02-06 16:36:36 +0800
Seen: 578 times
Last updated: Feb 18 '12