0

Inter Page communication during Page Loading Life cycle?

asked 2010-08-20 02:15:14 +0800

enix0907 gravatar image enix0907
129 1
http://sites.google.com/s...

In my code as follows,

I am confused when I can get page reference from desktop in onCreate() event of one of my included pages.
Why Page sourcePage = Executions.getCurrent().getDesktop().getPage("SchoAppForm"); and sourcePage is NULL?
I thought ALL components including pages have been created completely before calling onCreate() event.
Even if I try to use Defer mode: <include src="/share/SearchScholarship.zul" mode="defer" />, that page is still NULL.
I thought this zul page will be created as late as Rendering Phase and the other zul page can be created earlier that it....so should be working?

Please help me out.... I am stuck now.
Thank you very much!

Main Page:

<?page title="Create your application" contentType="text/html;charset=UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>


	<box id="appBox" apply="sas.application.CreateAppComposer">

		<vbox sclass="myInnerVBox" height="100%">
			<include src="/share/SearchScholarship.zul"/>

			<separator spacing="25px" bar="true" />

			<window title="Application" border="normal" width="100%"
				height="auto" contentStyle="overflow:auto">

				<include src="/share/form_ScholarshipApp.zul" />

			</window>
		</vbox>
	</box>
</zk>


SearchScholarship.zul:
<?page title="SearchScholarship" contentType="text/html;charset=UTF-8"?>

<window id="mainZK" apply="sas.scholarship.SearchSchoComposer">
	
	<!--do something here-->
                <listbox id="schoList" model="@{mainZK$composer.Scholarships}"
		width=" 945 px " rows="5" sizedByContent="false">
		<listhead>
			<listheader image="/images/ListBox-icon.png"
				label="Freshmen only scholarships" sort="auto" />
		</listhead>
		<listitem self="@{each='scho'}" value="@{scho}" height="25px">
			<listcell label="@{scho.Scho_Name}" />
		</listitem>

		<listfoot>
			<listfooter id="listFooter" />
		</listfoot>
	</listbox>
	
</window>


form_ScholarshipApp.zul:
<?page id="SchoAppForm" title="form_ScholarshipApp" contentType="text/html;charset=UTF-8"?>

<window id="mainWin" apply="sas.application.SchoAppFormComposer">
	
	<!--do something here-->
	
</window>


SearchSchoComposer.java:
public class SearchSchoComposer extends GenericForwardComposer {

        Listbox schoList;

	public void doAfterCompose(Component win) throws Exception {
		super.doAfterCompose(win);

	}

	public void onCreate() {	
		schoList.setSelectedIndex(0);
		Events.postEvent(new SelectEvent(Events.ON_SELECT, schoList, schoList
				.getSelectedItems()));
	}

	public void onSelect$schoList(SelectEvent eve) {
		Set<Listitem> items = eve.getSelectedItems();
		Listitem selectedItem = null;

		// this is single selection
		Iterator<Listitem> iterator = items.iterator();
		while (iterator.hasNext()) {
			selectedItem = iterator.next();
		}

		// get all text criteria of this selected scholarship(retrieve from DB
		// every time when selecting one scholarship)
		Scholarship selectedScho = (Scholarship) selectedItem.getValue();
		selectedScho.setTextCriteria(SchoTextCriteriaDB.Search(selectedScho));

		// post an event on another page to display this scholarship
		Page sourcePage = Executions.getCurrent().getDesktop().getPage("SchoAppForm"); ---> sourcePage is NULL !!
		Window win = (Window) sourcePage.getFellow("mainWin");
		Events.postEvent(new Event("onDisplay", win, selectedScho));
	}

}

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2010-08-20 07:06:22 +0800

RyanWu gravatar image RyanWu
533 2
about.me/flyworld

you can take a look these code :)

index.zul

<?page id="index_page" title="Create your application" contentType="text/html;charset=UTF-8"?>

<zk>
	<include src="SearchScholarship.zul" />
	<window id="wins" border="normal" width="100%" height="auto" contentStyle="overflow:auto">
		<include id="inc" src="form_ScholarshipApp.zul" />
	</window>
</zk>

SearchScholarship.zul

<?page title="SearchScholarship" contentType="text/html;charset=UTF-8"?>

<window id="mainZK" apply="org.zkoss.test.SearchSchoComposer">
	<button id="mybtn" label="getPage" />
</window>

form_ScholarshipApp.zul

<?page id="SchoAppForm" title="form_ScholarshipApp" contentType="text/html;charset=UTF-8"?>

<window id="mainWin">
	<button id="submit" label="submit" />
	<zscript><![CDATA[
	System.out.println("form : " + page.getId());
]]></zscript>
</window>

SearchSchoComposer.java


package org.zkoss.test;

import java.util.Collection;
import java.util.Iterator;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.Path;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Button;

public class SearchSchoComposer extends GenericForwardComposer {
	Button mybtn;

	public void doAfterCompose(Component win) throws Exception {
		super.doAfterCompose(win);

	}

	public void onCreate() {
		System.out.println("onCreate : " + page.getId());
		// System.out.println(page.getFellow("mainWin"));
	}

	public void onClick$mybtn() {
		Collection c = Executions.getCurrent().getDesktop().getPages();
		System.out.println("--ALL YOUR PAGE--");
		for (Iterator iterator = c.iterator(); iterator.hasNext();) {
			Page page = (Page) iterator.next();
			System.out.println(page.getId());
			// your <? page xxxx ?> in include file will be ignore, coz ZK page
			// is just like your "PAGE" only seen ONE !!
		}
		System.out.println("--END--");//Following code shows how to get the window "mainWin" in form zul
		System.out.println(page.getFellow("wins").getFellow("inc").getFellow("mainWin"));		
		System.out.println(Path.getComponent("/wins/inc/mainWin"));
				
	}

}

link publish delete flag offensive edit

answered 2010-08-20 13:31:31 +0800

enix0907 gravatar image enix0907
129 1
http://sites.google.com/s...

Hi RyanWu, thank you for your help and clarification. Now, I understand better :-)

However, in which situations we can get more than one page by using Executions.getCurrent().getDesktop().getPages();
After reading ZK5.0.3 API doc, when a include is Defer mode, is that will be one of the situations?

How about inline Macro? because I just relize a Instant Include works alike to regular Macro....

link publish delete flag offensive edit

answered 2010-08-26 20:14:47 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

Hi enix
You can refer to Inline Macros
It will not create a page

link publish delete flag offensive edit

answered 2010-08-26 21:03:18 +0800

enix0907 gravatar image enix0907
129 1
http://sites.google.com/s...

thank you Jimmy :)
I was thinking about using Macros instead <include>, and now still curious about which situations we can get more than one page by using Executions.getCurrent().getDesktop().getPages();
if all possible usages will turn out only one page.

link publish delete flag offensive edit

answered 2010-08-28 10:07:29 +0800

RyanWu gravatar image RyanWu
533 2
about.me/flyworld

updated 2010-08-28 10:07:38 +0800

@enix0907
here i comes again !!
my index.zul

<?page id="index_page" title="Create your application" contentType="text/html;charset=UTF-8"?>

<zk>
	<include src="SearchScholarship.zul" mode="auto"/>
	<!-- This will create a page !! -->
  <include src="SearchScholarship2.zul" mode="defer"/>	
	<window id="wins" border="normal" width="100%" height="auto" contentStyle="overflow:auto">
		<include id="inc" src="form_ScholarshipApp.zul" />
	</window>
</zk>

SearchScholarship2.zul is a copy version of SearchScholarship.zul , i only changed the composer apply="org.zkoss.test.SearchSchoComposer2"


SearchSchoComposer2.java

package org.zkoss.test;

import java.util.Collection;
import java.util.Iterator;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.Path;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Button;
import org.zkoss.zul.Window;

public class SearchSchoComposer2 extends GenericForwardComposer {
	Button mybtn;

	public void doAfterCompose(Component win) throws Exception {
		super.doAfterCompose(win);
		
	}

	public void onCreate() {
		System.out.println("onCreate2 : " + page.getId());
	}

	public void onClick$mybtn() {
		Collection c = Executions.getCurrent().getDesktop().getPages();
		System.out.println("--ALL YOUR PAGE--");
		for (Iterator iterator = c.iterator(); iterator.hasNext();) {
			Page page = (Page) iterator.next();
			System.out.println(page.getId());
			// your <? page xxxx ?> in include file will be ignore, coz ZK page
			// is just like your "PAGE" only seen ONE !!
		Window w = new Window();
		w.getPreviousSibling();
		}
		
		System.out.println("--END--");//Following code shows how to get the window "mainWin" in form zul
		
		//System.out.println(page.getFellow("wins").getFellow("inc").getFellow("mainWin"));  (you can't get by page because you have your own		
		System.out.println(Path.getComponent("//index_page/wins/inc/mainWin")); // Sure you can get other page's component
				
	}

}


now, you can have two pages using defer mode

link publish delete flag offensive edit

answered 2010-08-28 10:30:43 +0800

RyanWu gravatar image RyanWu
533 2
about.me/flyworld

updated 2010-08-28 10:34:00 +0800

let me talk about the macro component,

the inline macro -> you can image it is a include (auto)
the default macro ->you can image it is a include (defer) , but it does NOT create a Page.
It has it's own IdSpace, that means you can create a composer in a macro and get data from same ID component (COOL!)


in the document : ZK Basic conceptss

More generally, any component could form an ID space as long as it implements the org.zkoss.zk.ui.IdSpace interface. Page also implements the IdSpace interface, so it is also a space owner. Besides window and page, regular macro is another built-in space owner.


index2.zul

<?component name="test" inline="true" macroURI="/test/Inter_Page/macro.zul"?>
<?component name="test2"  macroURI="/test/Inter_Page/macro.zul"?>
<zk>
	<test macroURI="/test/Inter_Page/macro.zul" title="testTitle" />
	<!-- <test macroURI="/test/Inter_Page/macro.zul" title="testTitle" /> error --> 	
	<test2 macroURI="/test/Inter_Page/macro.zul" title="testTitle" />
	<test2 macroURI="/test/Inter_Page/macro.zul" title="testTitle" />
</zk>

macro.zul (why error with inline ? coz window have id="test" !!

<window id="test" border="normal" title="${arg.title}" width="200px">
  <label value="${empty arg.desc ? 'wow': arg.desc}" />
</window> 


now you can choose one from include/ Macro / Executions.getCurrent().createComponents to satisfied your project spec :)

link publish delete flag offensive edit

answered 2010-08-28 20:12:03 +0800

enix0907 gravatar image enix0907
129 1
http://sites.google.com/s...

I appreciate your well-defined examples to make me understand more about their differences and features. :=)

Thank you all again for helping me out!!!!!

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2010-08-20 02:15:14 +0800

Seen: 942 times

Last updated: Aug 28 '10

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More