Zk如何通过id到达包含的.zul页面组件?

发布于 2024-11-19 11:57:44 字数 6438 浏览 3 评论 0 原文

我无法通过包含的 .zul 页面中的 id 访问组件。我有一个带有控制器的 main.zul,我需要通过 java 控制器类获取包含的 zul 页面中的一个组件,但它返回 null。

我知道包含的方法会创建新的 id 空间,但是有什么方法可以获取这个组件吗?

更新

这是我的代码:

zul主页

<?page title="DealerVizard.zul"?>

<?page id="main" ?>

<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="./Dealer" ?>
<zk>
    <style src="/resources/css/default.css" />
    <window id="Dealer" class="index" 
        apply="com.i2i.prm.controller.IndexController">


        <div class="content" width="100%">

            <tabbox id="tb" forward="onSelect=onSelect">
                <tabs id="tabs">
                    <tab id="info" label="INFO" />
                    <tab id="create" label="CREATE" />
                    <tab id="edit" label="EDIT" />
                    <tab id="test" label="TEST PANEL(LIST BOX)" />

                </tabs>
                <tabpanels>
                    <tabpanel id="DealerInfo">
                        <include id="DealerInfoContent"
                            src="View/Dealer/DealerInfo.zul" />
                    </tabpanel>
                    <tabpanel id="DealerCreate">
                        <include id="DealerCreateContent"
                            src="View/Dealer/DealerCreate.zul" />
                    </tabpanel>
                    <tabpanel id="DealerEdit">
                        <include id="DealerEditContent"
                            src="View/Dealer/DealerEdit.zul" />
                    </tabpanel>

                    <tabpanel id="PagingListBox">
                        <include  id="PagingListBoxContent"  // Included here
                            src="View/TEST/PagingListBox.zul" />
                    </tabpanel>
                </tabpanels>
            </tabbox>
        </div>
    </window>

</zk>

PagingListBox.zul(包含页面)

<?page id="list" ?>

<zk>

    <grid width="100%">

        <columns>
            <column label="" />

        </columns>
        <rows>
            <row>
                <listbox id="listModel" width="100%" height="100%"
                    visible="true" span="true" pagingPosition="top" rows="20"
                    selectedItem="@{DealerController.selected}"
                    model="@{DealerController.userList}"
                    forward="onSelect=//main/Dealer.onSelect">
                    <auxhead>
                        <auxheader colspan="1">
                            <textbox
                                value="@{DealerController.searchUser.name}" maxlength="9"
                                id="searchCO_ID" forward="onChanging=//main/Dealer.onSearch"
                                width="100%">
                            </textbox>
                        </auxheader>
                        <auxheader colspan="1">
                            <textbox
                                value="@{DealerController.searchUser.surname}" maxlength="21"
                                id="searchMSISDN" forward="onChanging=//main/Dealer.onSearch"
                                width="100%">
                            </textbox>
                        </auxheader>
                        <auxheader colspan="1">

                        </auxheader>

                    </auxhead>

                    <listhead>
                        <listheader label="Name"
                            sort="auto(UPPER(name))" />

                        <listheader label="Surname"
                            sort="auto(UPPER(surname))" />


                        <listheader label="Delete ?" />
                    </listhead>


                    <listitem self="@{each=USERLIST}">

                        <listcell>
                            <label value="@{USERLIST.user.name}" />
                            <textbox
                                value="@{DealerController.tmpUser.name}" visible="false" />
                        </listcell>
                        <listcell>
                            <label value="@{USERLIST.user.surname}" />
                            <textbox
                                value="@{DealerController.tmpUser.surname}" visible="false" />
                        </listcell>

                        <listcell>
                            <button label="Update"
                                forward="onClick=//main/Dealer.onUpdate" visible="false" />
                            <button image="icons/edit-delete.png"
                                label="Delete" forward="onClick=//main/Dealer.onDelete"
                                width="100%" disabled="true" />
                            <button label="Save"
                                forward="onClick=//main/Dealer.onSave" visible="false" />
                            <button label="Cancel"
                                forward="onClick=//main/Dealer.onCancel" visible="false" />
                        </listcell>
                    </listitem>
                </listbox>
                <paging id="pagingData" pageSize="20" />
            </row>

        </rows>
    </grid>
</zk>

IndexCOntroller.java

public class IndexController extends  GenericForwardComposer  {

    private List<User> userList = new ArrayList<User>() ;
    AnnotateDataBinder binder;
    Tabbox tb;
    Window Dealer;
    private int pageCount=0;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        // TODO Auto-generated method stub
        super.doAfterCompose(comp);
        comp.setVariable(comp.getId() + "Controller", this, true);
        binder = (AnnotateDataBinder) Dealer.getVariable("binder", true);


    System.out.println(Path.getComponent("//list/listModel"));
    }


    public IndexController() {
        // TODO Auto-generated constructor stub
    }
}

I can't reach component by id in the included .zul page. I have one main.zul with a controller and I need to get a component in included zul page through the java controller class, but it returns null.

I know the included method creates new id space but is there any way to get this component?

UPDATE

Here is my code:

the main zul page

<?page title="DealerVizard.zul"?>

<?page id="main" ?>

<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="./Dealer" ?>
<zk>
    <style src="/resources/css/default.css" />
    <window id="Dealer" class="index" 
        apply="com.i2i.prm.controller.IndexController">


        <div class="content" width="100%">

            <tabbox id="tb" forward="onSelect=onSelect">
                <tabs id="tabs">
                    <tab id="info" label="INFO" />
                    <tab id="create" label="CREATE" />
                    <tab id="edit" label="EDIT" />
                    <tab id="test" label="TEST PANEL(LIST BOX)" />

                </tabs>
                <tabpanels>
                    <tabpanel id="DealerInfo">
                        <include id="DealerInfoContent"
                            src="View/Dealer/DealerInfo.zul" />
                    </tabpanel>
                    <tabpanel id="DealerCreate">
                        <include id="DealerCreateContent"
                            src="View/Dealer/DealerCreate.zul" />
                    </tabpanel>
                    <tabpanel id="DealerEdit">
                        <include id="DealerEditContent"
                            src="View/Dealer/DealerEdit.zul" />
                    </tabpanel>

                    <tabpanel id="PagingListBox">
                        <include  id="PagingListBoxContent"  // Included here
                            src="View/TEST/PagingListBox.zul" />
                    </tabpanel>
                </tabpanels>
            </tabbox>
        </div>
    </window>

</zk>

PagingListBox.zul (Included page)

<?page id="list" ?>

<zk>

    <grid width="100%">

        <columns>
            <column label="" />

        </columns>
        <rows>
            <row>
                <listbox id="listModel" width="100%" height="100%"
                    visible="true" span="true" pagingPosition="top" rows="20"
                    selectedItem="@{DealerController.selected}"
                    model="@{DealerController.userList}"
                    forward="onSelect=//main/Dealer.onSelect">
                    <auxhead>
                        <auxheader colspan="1">
                            <textbox
                                value="@{DealerController.searchUser.name}" maxlength="9"
                                id="searchCO_ID" forward="onChanging=//main/Dealer.onSearch"
                                width="100%">
                            </textbox>
                        </auxheader>
                        <auxheader colspan="1">
                            <textbox
                                value="@{DealerController.searchUser.surname}" maxlength="21"
                                id="searchMSISDN" forward="onChanging=//main/Dealer.onSearch"
                                width="100%">
                            </textbox>
                        </auxheader>
                        <auxheader colspan="1">

                        </auxheader>

                    </auxhead>

                    <listhead>
                        <listheader label="Name"
                            sort="auto(UPPER(name))" />

                        <listheader label="Surname"
                            sort="auto(UPPER(surname))" />


                        <listheader label="Delete ?" />
                    </listhead>


                    <listitem self="@{each=USERLIST}">

                        <listcell>
                            <label value="@{USERLIST.user.name}" />
                            <textbox
                                value="@{DealerController.tmpUser.name}" visible="false" />
                        </listcell>
                        <listcell>
                            <label value="@{USERLIST.user.surname}" />
                            <textbox
                                value="@{DealerController.tmpUser.surname}" visible="false" />
                        </listcell>

                        <listcell>
                            <button label="Update"
                                forward="onClick=//main/Dealer.onUpdate" visible="false" />
                            <button image="icons/edit-delete.png"
                                label="Delete" forward="onClick=//main/Dealer.onDelete"
                                width="100%" disabled="true" />
                            <button label="Save"
                                forward="onClick=//main/Dealer.onSave" visible="false" />
                            <button label="Cancel"
                                forward="onClick=//main/Dealer.onCancel" visible="false" />
                        </listcell>
                    </listitem>
                </listbox>
                <paging id="pagingData" pageSize="20" />
            </row>

        </rows>
    </grid>
</zk>

IndexCOntroller.java

public class IndexController extends  GenericForwardComposer  {

    private List<User> userList = new ArrayList<User>() ;
    AnnotateDataBinder binder;
    Tabbox tb;
    Window Dealer;
    private int pageCount=0;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        // TODO Auto-generated method stub
        super.doAfterCompose(comp);
        comp.setVariable(comp.getId() + "Controller", this, true);
        binder = (AnnotateDataBinder) Dealer.getVariable("binder", true);


    System.out.println(Path.getComponent("//list/listModel"));
    }


    public IndexController() {
        // TODO Auto-generated constructor stub
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寻找我们的幸福 2024-11-26 11:57:44

通常,我不建议使用 Path.getComponent() 方式访问其他组件,因为您的应用程序代码与视图页面中的组件结构紧密耦合。
在您的情况下,最简单的方法是使用 AbstractComponent#getFellow(String compId) 方法,例如。

Include inc = (Include) Dealer.getFellow("PagingListBoxContent");
Listbox listModel = (Listbox) inc.getFellow("listModel");
System.out.println(listModel);

因此,将来即使您在 ZUML 页面中的列表框之前插入任何其他组件,您的代码仍然可以工作。

更新:顺便说一句,有一个有趣的博文

Normally I wouldn't recommend using Path.getComponent() way to access other components as your application code becomes tightly coupled with your component structure in your view page.
In your case you simplest way is to use AbstractComponent#getFellow(String compId) method so for eg.

Include inc = (Include) Dealer.getFellow("PagingListBoxContent");
Listbox listModel = (Listbox) inc.getFellow("listModel");
System.out.println(listModel);

So in future even if you insert any other component in your ZUML page before your listbox your code will still work.

UPDATE: BTW there was an interesting blogpost on this very topic on ZK blog

奈何桥上唱咆哮 2024-11-26 11:57:44

如果你的include有id,你可以使用美元符号来获取内部组件

  <zk>
  <include id="inc" src="test.zul />

  </zk>

test.zul

    <zk>
    <label id="lab1" value="test1" />
    </zk>

你可以使用“inc$lab1”获取test.zul中的标签

if your include have id, you can use dollar sign to get the inner components

  <zk>
  <include id="inc" src="test.zul />

  </zk>

test.zul

    <zk>
    <label id="lab1" value="test1" />
    </zk>

you can use "inc$lab1" get the label in test.zul

塔塔猫 2024-11-26 11:57:44

您可以使用 zscript 或 java 访问任何其他 id 空间中的任何组件。如果它在同一页面上,但在不同的窗口上,则(窗口 A 中的组件 B):

Path.getComponent("/A/B");

如果它在不同的页面上,则(页面 P 上的窗口 A 中的组件 B):

Path.getComponent("//P/A/B");

您可以在此处找到文档: http://books.zkoss.org/wiki/ZK%20Developer%27s%20Reference/UI%20Comforming/ID%20Space

You can access any component in any other id space using zscript or java. if it is on the same page, but different window then (component B in window A):

Path.getComponent("/A/B");

if it is on a different page then (component B in window A on page P):

Path.getComponent("//P/A/B");

You can find documentation here: http://books.zkoss.org/wiki/ZK%20Developer%27s%20Reference/UI%20Composing/ID%20Space

逐鹿 2024-11-26 11:57:44

您可以在 IndexController.java 中添加:

...
   private Include DealerInfoContent;
...

这样您就可以访问父作曲家中包含的组件。

(不过,我建议使用驼峰命名法 id)。

You can add in your IndexController.java:

...
   private Include DealerInfoContent;
...

this way you can access the included component within the parent composer.

(I would suggest to use camelCase ids for it, though).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文