ZK绑定问题与字符串变量?

时间:2013-05-24 08:02:50

标签: java binding zk

我正在尝试将 Doublebox Decimalbox 等少数ZK组件与 String 变量绑定

package com.viewmodel;

import java.util.ArrayList;
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 com.binding.BindComponent;

public class BindingViewModel {
    BindComponent compo = null;
    private List<BindComponent> dataList;

    @AfterCompose
    public void afterCompose(@ContextParam(ContextType.VIEW) Component view){
        dataList = new ArrayList<BindComponent>();
        compo = new BindComponent();
        compo.setDecimalbox("12.67");
        compo.setTextbox("hariom");
        compo.setDoublebox("123");
        compo.setIntbox(12);
        dataList.add(compo);

    }

    public BindComponent getCompo() {
        return compo;
    }
    public void setCompo(BindComponent compo) {
        this.compo = compo;
    }
    public List<BindComponent> getDataList() {
        return dataList;
    }
    public void setDataList(List<BindComponent> dataList) {
        this.dataList = dataList;
    }


}

和ZUl页面..

<?page title="Auto Generated index.zul"?>
<window title="Hello World!!" apply="org.zkoss.bind.BindComposer"
    border="normal" width="200px"
    viewModel="@id('vm') @init('com.viewmodel.BindingViewModel')">

    <label value="You are using: ${desktop.webApp.version}" />

    <listbox model="@bind(vm.dataList)">
        <listhead>
            <listheader value="Textbox"></listheader>
            <listheader value="DoubleBox"></listheader>
            <listheader value="IntBox"></listheader>
            <listheader value="DecimalBox"></listheader>

        </listhead>
        <template name="model" var="mymodel">
            <listitem>
                <listcell>
                    <textbox value="@bind(mymodel.textbox)"></textbox>
                </listcell>
                <listcell>
                    <doublebox value="@bind(mymodel.doublebox)"></doublebox>
                </listcell>
                <listcell>
                    <intbox value="@bind(mymodel.intbox)"></intbox>
                </listcell>
                <listcell>
                    <decimalbox value="@bind(mymodel.decimalbox)"></decimalbox>
                </listcell>
            </listitem>
        </template>
    </listbox>
</window>

和Bean Class ....

public class BindComponent {
    private String textbox;
    private int intbox;
    private String doublebox;
    private String decimalbox;
    public String getTextbox() {
        return textbox;
    }
    public void setTextbox(String textbox) {
        this.textbox = textbox;
    }
    public int getIntbox() {
        return intbox;
    }
    public void setIntbox(int intbox) {
        this.intbox = intbox;
    }
    public String getDoublebox() {
        return doublebox;
    }
    public void setDoublebox(String doublebox) {
        this.doublebox = doublebox;
    }
    public String getDecimalbox() {
        return decimalbox;
    }
    public void setDecimalbox(String decimalbox) {
        this.decimalbox = decimalbox;
    }



}

现在的问题是,当我要在zul页面中更改值时,不调用set方法,因为我使用String变量绑定zul值。任何人都可以告诉我代码的问题是什么?

1 个答案:

答案 0 :(得分:0)

我认为问题是Decimalbox#getValue()返回BigDecimal 解决方案应该是

public void setDecimalbox(BigDecimal decimalbox) {
    this.decimalbox = decimalbox != null ? decimalbox.toString() : this.decimalbox;
}

对于Doublebox

public void setDoublebox(Double doublebox) {
    this.doublebox = doublebox != null ? doublebox.toString : this.doublebox;
}