Originally, to activate the Data Binding Manager, users have to define the page initializer at the top of the page like this:

<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>

This set of code creates an AnnotateDataBinder instance and set the instance as a variable named "binder" of the component, and then calls loadAll() to initiate all UI components from the associated data source. For more detailed information, please refer to ZK developer’s reference.

However, with ZK 5.0.8 there is now a new and cleverer way in which users can adopt to do achieve the same outcome – by defining a dedicated Composer – AnnotateDataBindingComposer. The code of AnnotateDataBindingComposer.java is simple and easy to read and understand.

public class AnnotateDataBindingComposer implements Composer, java.io.Serializable {
    protected AnnotateDataBinder _binder;
    public void doAfterCompose(Component comp) throws Exception {
        _binder = new AnnotateDataBinder(comp);
        comp.setAttribute("binder",_binder);
        _binder.loadAll();
    }
}

The old way:

<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>
<zk>
    <window  apply="org.zkoss.demo.MyComposer">
    </window>
</zk>

The equivalent new way:

<zk>
<window  apply="org.zkoss.demo.MyComposer,org.zkoss.zkplus.databind.AnnotateDataBindingComposer">
</window>
</zk>

 

Users are allowed to apply multiple composers to a component. For lifecycle issues, please apply AnnotateDataBindingComposer at the end.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

8 Responses to “Databinding Composer”

  1. Shumy says:

    Not exactly a new way. I do something similar since ZK 3.6

  2. TerryTornado says:

    Hmmm, i don’t see an advantage.

    What is with the params from the old way like this:

  3. TerryTornado says:

    [?init class=”org.zkoss.zkplus.databind.AnnotateDataBinderInit” arg0=”./windowCustomerAddon1″ ?]

  4. robertpic71 says:

    Sorry, i don’t see the “new” feature. This is possible since 3.x.

    Check the webside for my forum thread.

    However i prefer this way (i.e. to avoid binder.loadAll in some special cases….).

  5. Ondrej Medek says:

    I have created my own composer class MyComposer, subclass of GenericForwardComposer, where I do excatly the same binder init in the doAfterCompose method. Then all my composers are subclasses of MyComposer, so I do need neither AnnotateDataBinderInit nor multiple composer definition in the apply attribute. I just simple write

  6. mahesh says:

    nice and gentle blog very helpful
    databinding

  7. ferry says:

    very nice info..

  8. Senthil says:

    It is not working for me in zk 6 ce version.

    Here is the code

    It shows the following error message

    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    org.zkoss.zk.ui.UiException: class org.zkoss.zkplus.databind.AnnotateDataBinderInit must implement interface org.zkoss.zk.ui.util.Composer
    org.zkoss.zk.ui.impl.AbstractUiFactory.newComposer(AbstractUiFactory.java:121)
    org.zkoss.zk.ui.impl.AbstractUiFactory.newComposer(AbstractUiFactory.java:135)
    org.zkoss.zk.ui.impl.Utils.newComposer(Utils.java:81)
    org.zkoss.zk.ui.metainfo.ComponentInfo.toComposer(ComponentInfo.java:355)
    org.zkoss.zk.ui.metainfo.ComponentInfo.toComposers(ComponentInfo.java:323)
    org.zkoss.zk.ui.metainfo.ComponentInfo.resolveComposer(ComponentInfo.java:310)
    org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:775)
    org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:767)
    org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:676)
    org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:738)
    org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:698)
    org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:640)
    org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:391)
    org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:313)
    org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:214)
    org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:134)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.35 logs.

Leave a Reply