0

Is it possible to access a nested object using data binding?

asked 2009-12-11 08:04:55 +0800

fmcypriano gravatar image fmcypriano
612 1 7
http://felipecypriano.com...

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

delete flag offensive retag edit

4 Replies

Sort by ยป oldest newest

answered 2009-12-13 20:28:00 +0800

joylo0122 gravatar image joylo0122
688 1
www.zkoss.org

@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

link publish delete flag offensive edit

answered 2009-12-14 02:11:59 +0800

tomCat gravatar image tomCat
276 3

updated 2009-12-14 02:48:50 +0800

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 :( )

link publish delete flag offensive edit

answered 2009-12-14 03:17:42 +0800

joylo0122 gravatar image joylo0122
688 1
www.zkoss.org

@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

link publish delete flag offensive edit

answered 2009-12-14 11:44:12 +0800

fmcypriano gravatar image fmcypriano
612 1 7
http://felipecypriano.com...

Thanks Joy and tomCat.

My problem was a typo in zul binding, after comparing tomCat's code with mine I found it.

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-12-11 08:04:55 +0800

Seen: 750 times

Last updated: Dec 14 '09

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