Zk 包含的 .zul 页面与主控制器不兼容

发布于 2024-11-18 11:01:29 字数 4036 浏览 6 评论 0原文

我使用 tabbox 来创建选项卡式页面。每个选项卡都包含另一个 zul 页面。我有 1 个控制器应用于主页。

如果我在包含的 zul 页面上向组件添加一些操作,在控制器类上我无法捕获它。如果我将控制器应用于我的 zul,那么它会创建控制器类的新实例。

这是我的代码。

<zk>
    <style src="/resources/css/default.css" />
    <window id="Dealer" class="index"
        apply="com.i2i.prm.controller.IndexController" width="100%"
        height="100%">


        <div class="content" >

            <tabbox id="tb" width="100%" forward="onSelect=onSelect">
                <tabs id="tabs">
                    <tab id="info" label="INFO" />
                    <tab id="create" label="CREATE" />
                    <tab id="edit" label="EDIT" />

                </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>

                </tabpanels>
            </tabbox>
        </div>
    </window>

</zk>

这是我的控制器 (IndexController.java

<zk>
<window title="Dealer Edit"  >
    <grid width="100%" sizedByContent="true">
                            <columns>
                                <column label="" />

                            </columns>
                            <rows>
                                <row >
                                    <label value="Name"></label>
                                    <textbox
                                        value="@{DealerController.user.name }">
                                    </textbox>
                                </row>
                                <row>
                                    <label value="Surname"></label>
                                    <textbox
                                        value="@{DealerController.user.surname }" forward="onChange=onASD">
                                    </textbox>
                                </row>
                                <row>
                                    <label value="Address"></label>
                                    <textbox
                                        value="@{DealerController.user.address }">
                                    </textbox>
                                </row>

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

) 类:

public class IndexController extends GenericForwardComposer {

    private User user = new User();;
    AnnotateDataBinder binder;
    Tabbox tb;

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

        user.setName("Abdul");
        user.setSurname("Rezzak");
        user.setAddress("Giderken sağda");



        binder.loadAll();
    }

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

    public void onDFG(ForwardEvent event){
        System.out.println(this.hashCode());
    }

    public void onASD(ForwardEvent event){
        System.out.println(this.hashCode());
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

I used tabbox to create tabbed page. And each tab includes another zul page. I have 1 controller applied to main page.

If I add some action to component on included zul page, on controller class I cant catch it. If I apply controller to my zul then it creates new instance of controller class.

Here is my code.

<zk>
    <style src="/resources/css/default.css" />
    <window id="Dealer" class="index"
        apply="com.i2i.prm.controller.IndexController" width="100%"
        height="100%">


        <div class="content" >

            <tabbox id="tb" width="100%" forward="onSelect=onSelect">
                <tabs id="tabs">
                    <tab id="info" label="INFO" />
                    <tab id="create" label="CREATE" />
                    <tab id="edit" label="EDIT" />

                </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>

                </tabpanels>
            </tabbox>
        </div>
    </window>

</zk>

And dealerEdit.zul

<zk>
<window title="Dealer Edit"  >
    <grid width="100%" sizedByContent="true">
                            <columns>
                                <column label="" />

                            </columns>
                            <rows>
                                <row >
                                    <label value="Name"></label>
                                    <textbox
                                        value="@{DealerController.user.name }">
                                    </textbox>
                                </row>
                                <row>
                                    <label value="Surname"></label>
                                    <textbox
                                        value="@{DealerController.user.surname }" forward="onChange=onASD">
                                    </textbox>
                                </row>
                                <row>
                                    <label value="Address"></label>
                                    <textbox
                                        value="@{DealerController.user.address }">
                                    </textbox>
                                </row>

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

This is my controller (IndexController.java) class:

public class IndexController extends GenericForwardComposer {

    private User user = new User();;
    AnnotateDataBinder binder;
    Tabbox tb;

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

        user.setName("Abdul");
        user.setSurname("Rezzak");
        user.setAddress("Giderken sağda");



        binder.loadAll();
    }

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

    public void onDFG(ForwardEvent event){
        System.out.println(this.hashCode());
    }

    public void onASD(ForwardEvent event){
        System.out.println(this.hashCode());
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

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

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

发布评论

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

评论(1

何以笙箫默 2024-11-25 11:01:29
  1. 从包含的页面 (DealerEdit.zul) 中删除 ,因为它形成了自己的 IdSpace。不要忘记删除结束 标记。
  2. 更改您的 onASD 方法名称以包含您的 Include 组件 ID,即 onASD$DealerEditContent。看来 Include 也形成了自己的 IdSpace,并且转发事件无法跨 IdSpace

这应该可以工作。

更新 1:我刚刚确认 Include 是 IdSpace 所有者组件,因为它实现了 IdSpace 接口,因此这是您的情况的唯一解决方法。

更新 2:我发现一种更简单的方法来处理跨不同 IdSpace 的转发事件,即使用 ZUML 文件中的组件路径来指定目标组件。例如,在您的情况下,您可以在 main.zul 页面中指定页面 id

<?page id="main" ?>

,并在包含的页面(例如 DealerEdit.zul 页面)中转发事件时,

<textbox forward="onChange=//main/Dealer.onASD" />

其余代码将保持不变。

参考: http://books.zkoss.org/wiki/ZK_Developer%27s_Reference /Event_Handling/Event_Forwarding#Using_component_Path

  1. remove <window title="Dealer Edit" > from your included page (DealerEdit.zul) as it forms its own IdSpace. Don't forget to remove the closing </window> tag.
  2. change your onASD method name to include your Include component id i.e. onASD$DealerEditContent. It seems Include also form its own IdSpace and forward event does not work across IdSpace

This should work.

UPDATE 1: I just confirmed that Include is an IdSpace owner component as it implements IdSpace interface so this is the only workaround in your case.

UPDATE 2: I found one more easier way to deal with forwarding events across different IdSpace which is to use component Path within ZUML file for specifying target component. For example in your case you can specify page id in main.zul page

<?page id="main" ?>

and while forwarding event in your included page such as DealerEdit.zul page

<textbox forward="onChange=//main/Dealer.onASD" />

Rest of the code will remain the same.

Reference: http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/Event_Handling/Event_Forwarding#Using_component_Path

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