-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi all,
I'd like to use data binding to access a property of a object (different class) returnd by a get method. A sample code:
class Foo { public Bar getBar() {} } class Bar { public String getName() {} } <grid model="@{window$composer.fooList}"> <rows> <row self="@{each=foo}"> <label value="@{foo.bar.name}"/> </row> </rows> </grid>
If I do this I get an error saying that Foo (binding is looking the wrong class) doesn't have the method "name":
SEVERE: >>org.zkoss.zk.ui.UiException: java.lang.NoSuchMethodException: class Foo: name=name args=null
>>java.lang.NoSuchMethodException: class Foo: name=name args=null
>> at org.zkoss.lang.Classes.myGetAcsObj(Classes.java:954)
How could I access Bar's name through Foo getter?
Regards
@fmcypriano
Hi fmcypriano, I think "@{foo.bar.name}" isn't work because databinding won't know the value that Foo doesn't know.
It's impossible for class Foo to know what name is the bar unless you write a method return it. So i will suggest you
to write a method return the correct name of bar, and use "@{foo.xxx}" to get it.
/Joy
Hi,
"@{foo.bar.name}" should work. Here is a little example code, which works as expected:
Bar.java
public class Bar {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Foo.java:
public class Foo {
private Bar bar;
public void setBar(Bar bar){
this.bar = bar;
}
public Bar getBar() {
return bar;
}
}
MyController.java:
public class MyController extends GenericForwardComposer {
private List<Foo> fooList = new ArrayList<Foo>();
@Override
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
Foo foo = new Foo();
Bar bar = new Bar();
bar.setName("fooBar");
foo.setBar(bar);
fooList.add(foo);
DataBinder binder = new AnnotateDataBinder(comp);
binder.loadAll();
}
public List<Foo> getFooList() {
return fooList;
}
}
index.zul:
<window id="main" title="Hello World!!" border="normal" width="100%" apply="controller.MyController">
<grid model="@{main$composer.fooList}">
<rows>
<row self="@{each=foo}">
<label value="@{foo.bar.name}" />
</row>
</rows>
</grid>
</window>
Tested with zk5 and 3.6.3.
Best regards,
tomCat
(Sorry for the bad format, but the source code tag does not work :( )
@tomCat
Thanks tomCat!!
That's another way to make @{foo.bar.name} work and don't need to change code both in java class and zul page.
If the codes are not heavy, you can use my way, cause it's easier to write another method instead to create a composer, but i'll
need to modified codes in two places. If codes are heavy, tomCat give a great suggestion!
/Joy
Thanks Joy and tomCat.
My problem was a typo in zul binding, after comparing tomCat's code with mine I found it.
Asked: 2009-12-11 08:04:55 +0800
Seen: 750 times
Last updated: Dec 14 '09