-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi,
i receive the following Exception when i try to run savAll() of Databinder a second time.
Exception:
>>org.zkoss.zk.ui.UiException: Cannot find the specified databind bean expression:z_ba_91.age >> at org.zkoss.zkplus.databind.DataBinder.setBeanAndRegisterBeanSameNodes(DataBinder.java:1030) >> at org.zkoss.zkplus.databind.Binding.saveAttributeValue(Binding.java:423) >> at org.zkoss.zkplus.databind.Binding.saveAttribute(Binding.java:415) >> at org.zkoss.zkplus.databind.DataBinder.saveAttrs(DataBinder.java:570) >> at org.zkoss.zkplus.databind.DataBinder.saveComponent(DataBinder.java:533)
Here is my example code:
index.zul:
<window title="Hello World!!" border="normal" width="100%" apply="controller.MyController"> <listbox id="listbox" /> <button id="btn_save" label="Save" /> <button id="btn_add" label="Add" /> </window>
MyController.java:
public class MyController extends GenericForwardComposer { protected Listbox listbox; //autowired private List<Person> persons = new ArrayList<Person>(); private BindingListModelList list; private DataBinder binder; public MyController() { persons.add(new Person("Test1","1")); persons.add(new Person("Test2","2")); } @Override public void doAfterCompose(Component comp) throws Exception { super.doAfterCompose(comp); binder = new DataBinder(); list = new BindingListModelList(persons, true); listbox.setModel(list); listbox.setItemRenderer(new ListitemRenderer() { @Override public void render(Listitem item, Object data) throws Exception { Person person = (Person) data; binder.bindBean(item.getId(), person); Textbox name = new Textbox(); name.setValue(person.getName()); binder.addBinding(name, "value", item.getId() + ".name", null, null, "save", null, null, null, null); Listcell listcell = new Listcell(); name.setParent(listcell); listcell.setParent(item); Textbox age = new Textbox(); age.setValue(person.getAge()); binder.addBinding(name, "value", item.getId() + ".age", null, null, "save", null, null, null, null); listcell = new Listcell(); age.setParent(listcell); listcell.setParent(item); } }); } public void onClick$btn_save() { binder.saveAll(); } public void onClick$btn_add() { list.add(new Person("Test3","3")); }
When i run saveAll() only once, everything works fine. The exception only occurs when i add more Items to my list and try to run saveAll a second time.
Any ideas what am i doing wrong, or is it a bug?
Best regards
tomCat
Hi,tomCat
i try to rebulid DataBinder
binder = new DataBinder(); list = new BindingListModelList(persons, false); listbox.setModel(list); listbox.setItemRenderer(new ListitemRenderer() { @Override public void render(Listitem item, Object data) throws Exception { Person person = (Person) data; binder.bindBean(item.getId(), person); Textbox name = new Textbox(); name.setValue(person.getName()); binder.addBinding(name, "value", item.getId() + ".name", null, null, "save", null, null, null, null); Listcell listcell = new Listcell(); name.setParent(listcell); listcell.setParent(item); Textbox age = new Textbox(); age.setValue(person.getAge()); binder.addBinding(age, "value", item.getId() + ".age", null, null, "save", null, null, null, null); //modify name to age listcell = new Listcell(); age.setParent(listcell); listcell.setParent(item); } }); } public void onClick$btn_save() { binder.saveAll(); } public void onClick$btn_add() { binder=new DataBinder(); persons.add(new Person("Test3","3")); list.clear(); list.addAll(persons); }
Hi as1225,
thanks for your tip. The solution works, but will force rerender of the complete listbox.
Any other solutions?
(I still believe that this behaviour is a bug. Can anybody confirm this?)
Best regards
tomCat
Hi jimmyshiau ,
Can you answer my question related to databinding Here
Thanks
Hi jimmyshiau,
Please have a look on this example...I am trying to make a Simple BInding Example but not able to do that
My Java Class
package com.test.binding; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import org.zkoss.bind.annotation.AfterCompose; import org.zkoss.bind.annotation.Command; import org.zkoss.bind.annotation.ContextParam; import org.zkoss.bind.annotation.ContextType; import org.zkoss.zk.ui.Component; import org.zkoss.zkplus.databind.AnnotateDataBinder; import org.zkoss.zkplus.databind.Binding; import org.zkoss.zkplus.databind.BindingListModelList; import org.zkoss.zul.ListModel; import org.zkoss.zul.ListModelList; import org.zkoss.zul.Listcell; import org.zkoss.zul.Listitem; import org.zkoss.zul.ListitemRenderer; import org.zkoss.zul.Textbox; public class TestRenderer { ListModelList model = new ListModelList(); private AnnotateDataBinder binder; @AfterCompose public void afterCompose(@ContextParam(ContextType.VIEW) Component view) { binder = new AnnotateDataBinder(view); List persons = new ArrayList(); model.add(new Person("David", "Coverdale")); model.add(new Person("Doug", "Aldrich")); model.add(new Person("Reb", "Beach")); model.add(new Person("Michael", "Devin")); model.add(new Person("Brian", "Tichy")); } public void setModel(ListModelList model) { this.model = model; } public ListModel getModel() { return model; } // method for ZK 6 public ListitemRenderer getItemRenderer() { ListitemRenderer _rowRenderer = null; if (_rowRenderer == null) { _rowRenderer = new ListitemRenderer() { public void render(final Listitem item, Object object, int index) throws Exception { final Person dataBean = (Person) object; binder.bindBean(item.getId(), dataBean); Listcell cell = new Listcell(); Textbox name = new Textbox(); name.setValue(dataBean.getFirstName()); binder.addBinding(name, "value", item.getId() + ".firstName", null, null, "save", null, null, null, null); cell.appendChild(name); cell.setParent(item); binder.bindBean(item.getId(), dataBean); } }; binder.saveAll(); binder.loadAll(); } return _rowRenderer; } @Command public void onOK() { binder.loadAll(); binder.saveAll(); // load Inputfields from Form, Constraints will be // performed } public class Person { private String firstName; private String lastName; public Person(String fn, String ln) { setFirstName(fn); setLastName(ln); } public String getFirstName() { return firstName; } public void setFirstName(String fn) { firstName = fn; } public String getLastName() { return lastName; } public void setLastName(String ln) { lastName = ln; } } }
And Zul Page
<zk> <window border="normal" title="hello" apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('com.test.binding.TestRenderer')" > <button label="ClickMe" id="retrieve" onClick="@command('onOK')"> </button> <div height="800px"> <listbox model="@load(vm.model)" itemRenderer="@load(vm.itemRenderer)" vflex="true" multiple="true"/> </div> </window> </zk>
Can you please answer my two question
1-Why i am getting Exception ?
Cannot find the specified databind bean expression:.firstName
2-How can i get change value of textbox in java class as I Changed David to Hariom but i was not able to get changed value in Java Class?
Thanks
Hi srossi
@AfterCompose public void afterCompose(@ContextParam(ContextType.VIEW) Component view) { binder = new AnnotateDataBinder(view);
You shall not create another binder, you shall retrieve the binder from ContextParam, please refer to the following link
http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/MVVM/Syntax/ViewModel/Parameters/@ContextParam#Example
@Init public void init( @ContextParam(ContextType.BINDER) Binder binder) { this.binder = binder; }
Asked: 2009-09-04 09:20:55 +0800
Seen: 769 times
Last updated: Dec 19 '12