-
FEATURED COMPONENTS
First time here? Check out the FAQ!
<listbox id="lbActiveSite" checkmark="true" rows="4" model="@{productWin$composer.availableSites}" width="100%" multiple="true" selectedItem="@{productWin$composer.product.sites, converter= 'fi.uwasa.netmes.util.SelectedItemsConverter'}">
package fi.uwasa.netmes.util; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.Executions; import org.zkoss.zk.ui.UiException; import org.zkoss.zk.ui.event.Events; import org.zkoss.zk.ui.event.SelectEvent; import org.zkoss.zkplus.databind.BindingListModel; import org.zkoss.zkplus.databind.TypeConverter; import org.zkoss.zul.ListModel; import org.zkoss.zul.Listbox; import org.zkoss.zul.Listitem; /** * Convert selected items to bean and vice versa. * */ public class SelectedItemsConverter implements TypeConverter, java.io.Serializable { private static final long serialVersionUID = 1L; public Object coerceToUi(Object val, Component comp) { //load Listbox lbx = (Listbox) comp; lbx.clearSelection(); if (val != null && val instanceof Collection) { Collection<Object> values = (Collection) val; Iterator<Object> valuesIterator = values.iterator(); Set<Object> items = new HashSet<Object>(); final ListModel xmodel = lbx.getModel(); if (xmodel instanceof BindingListModel) { final BindingListModel model = (BindingListModel) xmodel; Listitem item = null; while (valuesIterator.hasNext()) { Object value = valuesIterator.next(); int index = model.indexOf(value); if (index >= 0) { item = (Listitem) lbx.getItemAtIndex(index); if (item != null) { items.add(item); } } } //We need this to support load-when:onSelect when first load //the page in (so it is called only once). if (items.size() > 0 & items.size() != lbx.getSelectedCount()) { // bug 1647817, avoid endless-loop //bug #2140491 Executions.getCurrent().setAttribute("zkoss.zkplus.databind.ON_SELECT"+lbx.getUuid(), Boolean.TRUE); Events.postEvent(new SelectEvent("onSelect", lbx, items, item)); } lbx.setSelectedItems(items); return TypeConverter.IGNORE; } else if (xmodel == null) { //no model case, assume Listitem.value to be used with selectedItem //iterate to find the selected item assume the value (select mold) while (valuesIterator.hasNext()) { Object value = valuesIterator.next(); for (Iterator it = lbx.getItems().iterator(); it.hasNext();) { Listitem item = (Listitem) it.next(); if (value.equals(item.getValue())) { items.add(item); } } } lbx.setSelectedItems(items); return TypeConverter.IGNORE; } else { throw new UiException("model of the databind listbox "+lbx+" must be an instanceof of org.zkoss.zkplus.databind.BindingListModel." + xmodel); } } return null; } public Object coerceToBean(Object val, Component comp) { //save final Listbox lbx = (Listbox) comp; if (Executions.getCurrent().getAttribute("zkoss.zkplus.databind.ON_SELECT"+lbx.getUuid()) != null) { //bug #2140491 //triggered by coerceToUi(), ignore this Executions.getCurrent().removeAttribute("zkoss.zkplus.databind.ON_SELECT"+lbx.getUuid()); return TypeConverter.IGNORE; } if (val != null) { final ListModel model = lbx.getModel(); Set<Listitem> listitems = (Set<Listitem>) lbx.getSelectedItems(); Iterator<Listitem> itemsIterator = listitems.iterator(); HashSet<Object> selectedValues = new HashSet<Object>(); while (itemsIterator.hasNext()) { Listitem listitem = itemsIterator.next(); if (model != null) { // model case selectedValues.add(model.getElementAt(listitem.getIndex())); } else { // no model case --> Value selectedValues.add(listitem.getValue()); } } return selectedValues; } return null; } }
code: nullSEVERE: >>org.zkoss.zk.ui.UiException: java.lang.ClassNotFoundException: >>java.lang.ClassNotFoundException: >> at org.zkoss.zkplus.databind.Binding.setConverter(Binding.java:307) >> at org.zkoss.zkplus.databind.Binding.<init>(Binding.java:133) >> at org.zkoss.zkplus.databind.DataBinder.addBinding(DataBinder.java:375) >> at org.zkoss.zkplus.databind.AnnotateDataBinder.loadComponentPropertyAnnotationByAnnotName(AnnotateDataBinder.java:424) >> at org.zkoss.zkplus.databind.AnnotateDataBinder.loadComponentPropertyAnnotation(AnnotateDataBinder.java:413) >> at org.zkoss.zkplus.databind.AnnotateDataBinder.loadAnnotations(AnnotateDataBinder.java:404) >>... [/code What I have done wrong ? Thank you very much for your help.
Hi vinhvo
ClassNotFoundException happen on which line ?
What version are you using ?
ZK 5?
You can refer to here
Hi, Thank you as1225 . YEs , I do use ZK 5 . <listbox id="lbActiveSite" checkmark="true"
rows="4" model="@{productWin$composer.availableSites}"
width="100%" multiple="true" selectedItems="@{productWin$composer.product.sites, converter= 'fi.uwasa.netmes.util.SelectedItemsConverter'}">
The bolded part is where I have problem. It is bean converter which convert my 'site' bean to ListItem.
As I followed this thread :Here. However, I faced SEVERE: >>org.zkoss.zk.ui.UiException: java.lang.ClassNotFoundException: when I use binder.loadAll().
please remove space after =
converter= 'fi.uwasa.netmes.util.SelectedItemsConverter'} converter='fi.uwasa.netmes.util.SelectedItemsConverter'}
Asked: 2010-05-29 02:10:24 +0800
Seen: 1,875 times
Last updated: Jun 02 '10