zk mvc 2 .zul共享同一个控制器

时间:2012-06-20 03:22:49

标签: java spring zk

如果我有2个.zul,一个有一个列表框,另一个是模态窗口。他们可以使用相同的控制器吗?我无法让它发挥作用。

问题基本上恢复了,我是否必须在两个.zul中使用apply="myController"? 如果我这样做,我在myList得到nullpointerException,因为我认为zk实例化了2个控制器对象,并且模态不存在myList。但如果我不在modal.zul上应用控制器,按下按钮时没有任何反应。

如何让2 .zul使用相同的控制器实例?

mail.zul

<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
<zk>
    <hlayout>
    <div id="winDiv" apply="com.company.controller.ProductController">
        <vbox>
            <listbox id="myList" width="690px" height="300px" >
                <listhead>
                    <listheader hflex="min" label="id" sort="auto(id)" />
                    <listheader hflex="2" label="name" sort="auto(name)" />
                    <listheader hflex="4" label="description" sort="auto(description)" />
                                        <listheader hflex="min" label="opcion"/>
                                </listhead>
            </listbox>
        </vbox>
    </div>
            <button label="new" id="new"/>
        </hlayout>
</zk>

modal.zul

<?xml version="1.0" encoding="UTF-8"?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<window id="modalProductType" title="Nuevo tipo de producto" border="normal" width="420px"
    closable="true" position="center,center" **apply="com.company.controller.ProductController"**>

    <grid>
        <columns>
                <column hflex="1"/>
                <column hflex="2"/>
        </columns>
        <rows>
                <row>
                    Clave:
                     <textbox hflex="1" value="@{product.id}" readonly="true"/>
                </row>
                <row>
                    Nombre:
                     <textbox id="txtname" value="@{product.name}" hflex="1" tabindex="1" />
                </row>
                <row>
                     Description:
                      <textbox value="@{product.description}" rows="5" hflex="1" tabindex="2" />
                </row>
                <row>
                    <cell colspan="2" style="text-align:center">
                        <hlayout>
                            <button width="100px" id="save" label="Aceptar"/>
                            <button width="100px" label="close"/>
                        </hlayout>
                   </cell>
                </row>
        </rows>
    </grid>
</window>

ProductController.java

public class ProductTypeController extends SelectorComposer {
@WiredVariable
    private ProductTypeService productTypeService;

@Wire
    private Listbox myList;

//methods...

}

1 个答案:

答案 0 :(得分:0)

不建议这样做

  1. 避免并发问题(参考here)例如。如果将相同的控制器实例应用于多个视图,则有线组件将指向导致UI不稳定行为的最后一个视图。使用不同的实例更容易,而不是编写并发问题并使控制器线程安全。
  2. 避免内存泄漏。例如。如果相同的控制器实例应用于两个完全不同的视图,并且两个视图中的组件都连接在其中,那么如果其中一个视图未被使用,则其连接的组件仍将不会被释放,从而导致内存泄漏。
相关问题