0

Problem with DataBinder.saveAll

asked 2009-09-04 09:20:55 +0800

tomCat gravatar image tomCat
276 3

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

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2009-09-04 17:29:01 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

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);			
	}


it will addBinding again for new DataBinder

link publish delete flag offensive edit

answered 2009-09-16 09:12:23 +0800

tomCat gravatar image tomCat
276 3

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

link publish delete flag offensive edit

answered 2009-09-23 06:33:57 +0800

tomCat gravatar image tomCat
276 3

I am still facing this serious problem...
No more ideas?

Best regards

tomCat

link publish delete flag offensive edit

answered 2012-12-11 09:20:16 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

updated 2012-12-11 09:20:37 +0800

Hi jimmyshiau ,
Can you answer my question related to databinding Here
Thanks

link publish delete flag offensive edit

answered 2012-12-12 06:30:17 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

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

link publish delete flag offensive edit

answered 2012-12-12 09:08:24 +0800

srossi gravatar image srossi
55 1

Tomcat, i think we have the same problem, i did some homeworks here

i personally think its a bug
Regards
Stefano

link publish delete flag offensive edit

answered 2012-12-18 01:56:18 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

updated 2012-12-18 02:18:37 +0800

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;
}

link publish delete flag offensive edit
Your reply
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

RSS

Stats

Asked: 2009-09-04 09:20:55 +0800

Seen: 768 times

Last updated: Dec 19 '12

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