Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ZK:如何创建组件和注册数据绑定_Java_Data Binding_Zk - Fatal编程技术网

Java ZK:如何创建组件和注册数据绑定

Java ZK:如何创建组件和注册数据绑定,java,data-binding,zk,Java,Data Binding,Zk,嗨 我正在使用开发web应用程序。我使用组件上的数据绑定来设置和获取值。我可以在源ZUL文件中注册数据绑定,也可以在页面控制器中的方法doAfterCompose中注册数据绑定。在页面合成期间调用此方法。但现在我必须添加一个新组件,它将数据绑定到现有的和组合的页面中。如何创建组件我知道,这很简单,但我有一个注册数据绑定的问题。框架抛出一个异常,即我正在使用未知数据bean 有一个简单的代码应该可以工作,但它不能。这个ZUL文件描述了简单的页面布局和控制器捕捉事件等。其中使用了注释ZkModel和

我正在使用开发web应用程序。我使用组件上的数据绑定来设置和获取值。我可以在源ZUL文件中注册数据绑定,也可以在页面控制器中的方法doAfterCompose中注册数据绑定。在页面合成期间调用此方法。但现在我必须添加一个新组件,它将数据绑定到现有的和组合的页面中。如何创建组件我知道,这很简单,但我有一个注册数据绑定的问题。框架抛出一个异常,即我正在使用未知数据bean

有一个简单的代码应该可以工作,但它不能。这个ZUL文件描述了简单的页面布局和控制器捕捉事件等。其中使用了注释ZkModel和ZkEvents。这些注释不是框架的一部分,它们是我们的增强。ZkModel将变量发布到ZUL文件,以便使用数据绑定从ZUL文件访问该变量。ZkEvent在组件上注册事件,并在事件上调用这些方法。这些增强功能正在发挥作用,因此没有问题

ZUL文件(test.ZUL):

如果我运行这两个文件,应用程序工作正常。OnOnClick按钮上的事件创建新的文本框,其值绑定到right属性。但现在,如果我在ZUL文件中注释textbox组件

        <!--textbox id="txt1" value="@{ctl.bindingValue}"/-->

这就是问题所在。我需要能够创建一个新组件并添加其数据绑定,而不必使用ZUL文件中的bean。我需要能够从控制器注册它。你能帮帮我吗?非常感谢。

不幸的是,当前的DataBinder还不支持动态绑定(必须等待Data binding 2.0,希望在ZK 5.5中可用)

在第一次调用getXxx()或setXxx()后,当前DataBinder实现将忽略所有后来添加的绑定。在这些方法中,init()方法是按需调用的,如果被调用,则不会再次调用。在init()方法中,基本上DataBinder扫描所有绑定并构造内部数据结构,然后使用这些内部数据结构(这就是为什么DataBinder看不到后来添加的绑定)


希望这能澄清您的问题。

在zk 8中,这可以通过模板机制和viewmodel概念的
@load
@bind
功能实现:

非常感谢,这证实了我的想法。我发现绑定树是在页面组合期间构建的,之后它似乎是不可更改的。我花了很多时间试图将一些数据bean添加到现有的内部结构中,但没有成功。我们已经重写了ZK框架的一部分(也是绑定的一部分),但是这个问题还没有解决。我必须找到其他解决办法。再次非常感谢
package cz.datalite.bpej.evidence;

import cz.datalite.stereotype.Controller;
import cz.datalite.zk.annotation.ZkEvent;
import cz.datalite.zk.annotation.ZkModel;
import cz.datalite.zk.components.textbox.DLTextbox;
import cz.datalite.zk.composer.DLComposer;
import java.util.HashMap;
import java.util.Map;
import org.zkoss.zk.ui.Component;
import org.zkoss.zkplus.databind.Binding;
import org.zkoss.zkplus.databind.DataBinder;
import org.zkoss.zul.impl.XulElement;

@Controller // this class serves as a controller
public class TestController extends DLComposer {

    @ZkModel // this property is published and accessible from ZUL file
    String bindingValue = "there is binding text";

    @ZkEvent( id = "btn" ) // this methods is invoked on "onClick" event on component "btn"
    public void onOk() throws Exception {
        DLTextbox textbox = new DLTextbox();
        textbox.setParent( self );
        setValueAnnotation( textbox, "value", "ctl.bindingValue" );    
    }

    /**
     * Sets the component's annotation to specific value
     * (call eg. setValueAnnotation(comp, "model", "aaa") is corresponding to model="@{aaa}")
     * @param comp defined component
     * @param propName name of property
     * @param annot annotation
     */
    private void setValueAnnotation( XulElement comp, String propName, String annot ) {
        DataBinder binder = ( DataBinder ) comp.getVariable( "binder", false );

        // adds new binding
        Map attrs = new HashMap();
        attrs.put( "value", annot );
        binder.addBinding( comp, propName, annot );

        // if the first bean is fellow then register it ( if it hasn't been used yet then it is not registered. )
        String bean = annot;
        if ( bean.contains( "." ) ) {
            bean = bean.split( "\\." )[0];
        }
        Component fellowBean = comp.getFellowIfAny( bean );
        if ( fellowBean != null ) {
            binder.bindBean( bean, fellowBean );
        }

        // load components value
        Binding bind = (( DataBinder ) comp.getVariable( "binder", false )).getBinding( comp, propName );
        if ( bind != null ) {
            bind.loadAttribute( comp );
        }

    }
}
        <!--textbox id="txt1" value="@{ctl.bindingValue}"/-->
org.zkoss.zkplus.databind.DataBinder(DataBinder.java#myGetBeanWithExpression:1004)
org.zkoss.zkplus.databind.DataBinder(DataBinder.java#getBeanAndRegisterBeanSameNodes:988)
org.zkoss.zkplus.databind.Binding(Binding.java#loadAttribute:413)
cz.datalite.bpej.evidence.TestController(TestController.java#setValueAnnotation:58)
cz.datalite.bpej.evidence.TestController(TestController.java#onOk:25)