使用 zul 文件中的路径 Path("//pg_id/window") 访问组件时出现 NullPointerException

NullPointerException when access component using path Path("//pg_id/window") in zul file

需要帮助我想从 login.zul 访问一些包含在 main.zul 文件中的组件,我想在用户是时将我的 user_id 存储在可序列化 class 中已登录,这是我的代码 login.zul :

<?page id="pg_login"?>
<zk xmlns="http://www.zkoss.org/2005/zul">
   <window id="wlogin" title="Login"  style="border:0px;background:#FF9900;" width="350px" height="340px"  closable="false" maximizable="false" mode="modal" maximized="false" position="center" apply="id.my.berkah.web.dmt.controller.LoginCTRL" sizable="false">      
       <grid height="100%" width="100%" style="border:0px;background:#FF0000;" oddRowSclass="non-odd">
            <columns>
               <column align="right" width="120px" style="border:0px;"/>
               <column align="left"  width="216px" style="border:0px;"/>
            </columns>
            <rows >
                <row spans="2">
                   <image src="img/LogoDmt1.png" height="180px" width="330px"/>
                   <!--<label value="DMT" />-->
                </row> 
                <row style="border:0px;background:#FF0000;">
                  <label style="color: white" value="User :"/>  
                  <textbox id="txtUser" width="200px" style="text-transform: uppercase" height="18px"/>
                </row>
                <row style="border:0px;background:#FF0000;">
                  <label style="color: white" value="Password :"/>
                  <textbox id="txtPassword" type="password" width="200px" height="18px"/>
                </row>
                <row visible="false">
                    <textbox id="status" value="N"/>
                    <textbox id="sessionId"/>
                </row>
                <row  spans="5" align="left">                  
                    <hbox> 
                        <div width="115px"></div>  
                    <button id="btnlogin"  label="Login" image="/img/Lock24.png" height="35px" width="100px"/> 
                    <button id="btncancel" label="Cancel" image="/img/Logout24.png" height="35px" width="100px"/>           
                    </hbox>
                </row>
            </rows>
       </grid>
    </window>
</zk>

这是我的 main.zul :

<?page id="pg_index" cacheable="false" language="xul/html" zscriptLanguage="Java" contentType="text/html;charset=UTF-8"?>
<zk xmlns="http://www.zkoss.org/2005/zul">
    <style>   
        .nav_toolbar .z-toolbarbutton-content i { font-size: 20px;}
        .nav_tabs .z-tabpanel { padding: 0; border-bottom: 0 none;}
        .nav_tabs .z-tabpanels .z-toolbarbutton-content { padding: 4px;}
        .nav_category { cursor:pointer; }
        .nav_text { color: #696569; font-size : 24px; font-weight: bold;}
    </style>
    <style>
        .labelstyle {
        font-weight: bold;
        font-size  : 30px;
        font-align : center;
        font-family: 'Times New Roman';
        color: green;
        }
    </style>
    <div id="container" height="50px" width="100%"> 
        <borderlayout>            
    <center>       
                <!--<label id="lblx" value="DATA MEDIA TELEKOMUNIKASI"  sclass="labelstyle"/>-->               
    </center>
        </borderlayout>
    </div>
    <toolbar sclass="nav_toolbar">
        <toolbarbutton target="_zkdemo" style="color: #3a92c8;"
                           iconSclass="z-icon-twitter-square" href="http://twitter.com/zkoss" />
        <toolbarbutton target="_zkdemo" style="color: #3b5998;"
                           iconSclass="z-icon-facebook-square" href="http://www.syamjavaneze@facebook.com" />
        <toolbarbutton target="_zkdemo" style="color: #696569;"
                           iconSclass="z-icon-github-square" href="https://github.com/zkoss/" />

    </toolbar>
    <separator height="5px">
    </separator>

    <!--<include id="includeLogin" mode="instant" src="login.zul" />-->
    <window id="win_main">
        <zscript><![CDATA[
        void updateCategory() {
        //includeID.setSrc("/findMonitor.zul");
        }
        void updateSubPage() {
            iframe.setSrc("/login.zul");
        }
        void addListTrx() {
            iframe.setSrc("/list_assign_msisdn_dpa.zul");
        }
    ]]>
    </zscript>
        <borderlayout height="650px" >
            <west title="Menu" size="20%" flex="true" maxsize="200" splittable="true" autoscroll="true" collapsible="true">
                <div  width="50%" height="300px"  style="background:#24a0aa;"> 
                    <hbox>
                        <navbar id="vn" orient="vertical" collapsed="false" width="300px"  >    
                            <navitem label="Home" iconSclass="z-icon-home"  />
                            <nav label="Get Started" iconSclass="z-icon-th-list"  badgeText="3" >
                                <navitem label="Step One" />
                                <navitem label="Step Two" />
                                <navitem label="Step Three" />
                            </nav>
                            <navitem label="About" iconSclass="z-icon-flag"/>
                            <navseparator/>
                            <nav label="Contact" iconSclass="z-icon-envelope" badgeText="5">
                                <navitem label="Reply" iconSclass="z-icon-reply" />
                                <navitem label="Reply all" iconSclass="z-icon-reply-all"/>
                                <navitem label="Inbox" iconSclass="z-icon-inbox"/>
                                <navitem label="Edit" iconSclass="z-icon-edit"/>
                                <navseparator/>
                                <nav label="Settings" iconSclass="z-icon-cog" badgeText=">>">
                                    <navitem label="Edit profile" iconSclass="z-icon-user"/>
                                    <navitem label="Keyboard shortcuts" iconSclass="z-icon-keyboard-o"/>
                                    <navitem label="Change password" iconSclass="z-icon-key"/>
                                </nav>
                            </nav>     
                            <navitem id="menuList" label="redirect" context="editPopup" onClick="Executions.getCurrent().sendRedirect(&quot;list_assign_msisdn_dpa.zul&quot;, &quot;_blank&quot;);"/>
                            <navitem label="href" context="editPopup" onClick="updateSubPage()"/>
                            <navitem label="Freeze" iconSclass="z-icon-asterisk" onClick="addListTrx()" />
                            <navitem label="Logout"  onClick="updateSubPage()" iconSclass="z-icon-power-off"/>
                        </navbar>
                        <menupopup id="editPopup">                               
                        <menuitem label="Buka di tab baru" onClick="Executions.getCurrent().sendRedirect(&quot;list_assign_msisdn_dpa.zul&quot;, &quot;_blank&quot;);" />
                        <menuitem label="Help" onClick="Messagebox.show(&quot;Silahkan hubungi kantor polisi&quot;)"/>
                        </menupopup>
                    </hbox>
                </div>
            </west>      
            <center flex="true" autoscroll="true"  style="background:#5C899B;">        
                <borderlayout> 
                    <center title="Content Area" vflex="true" >
                    <iframe id="iframe" 
                    width="100%" height="700px" style="border: 1px solid gray" />
                    </center> 
                    <south id="planFrame"   vflex="true">
                    </south>
                </borderlayout>  
            </center>
            <north>
                <label  id="g1" value="User Name" style="text-transform: uppercase;color:white;font-weight:bold;cursor:hand;cursor:pointer" height="50px" sclass="bawah" tooltiptext="Click Here for User Information and Request BU."/>   
            </north>  
        </borderlayout>
    </window>
    <include id="includeID" mode="instant" src="/login.zul"  />    
    </zk>    

我无法使用此 java 代码获取 txt 密码:

DMTImplements imp=new DMTImplements();
Path parent = new Path("//pg_login/wlogin");
Textbox pass=(Textbox)new Path(parent,"txtPassword").getComponent();

异常消息是:

Exception in thread "main" java.lang.NullPointerException
    at org.zkoss.zk.ui.Path.getComponent0(Path.java:114)
    at org.zkoss.zk.ui.Path.getComponent(Path.java:66)

我该怎么办??

问题在于您包含登录页面的模式。来自关于 mode="instant":

的 zk 文档

Unlike the defer mode, no additional Page instance is created.

所以included zul直接添加到include zul下。如果你还不知道,

the include component itself is a ID space owner

使用 mode="defer",包含组件将替换为您包含的页面。

因此 <include> 组件有 2 个选项:

模式="instant"

<include id="includeID" mode="instant" src="/login.zul"  />

您必须从主页开始浏览所有 IDspace 所有者。在你的情况下:

Path parent = new Path("//pg_index/includeID/wlogin");

或者,因为只有一页:

Path parent = new Path("/includeID/wlogin");

模式="defer"

<include id="includeID" mode="defer" src="/login.zul"  />

您的登录页面是与您的索引页面一起创建的,因此您的原始代码可以正常工作:

Path parent = new Path("//pg_login/wlogin");

你的选择 ;-)

亚历克斯