Implicit Objects"

From Documentation
m (correct highlight (via JWB))
 
(4 intermediate revisions by 3 users not shown)
Line 2: Line 2:
  
 
== Overview ==
 
== Overview ==
For scripts embedded in a ZUML page, there are a set of implicit objects that enable developers to access components more efficiently. It's also a mechanism to pass data inter page, application. These objects are available to the Java codes included by the <tt>zscript</tt> element and the attributes for specifying event listeners. They are also available to EL expressions.
+
For scripts embedded in a ZUML page, there are a set of implicit objects that enable developers to access components more efficiently. It's also a mechanism to pass data inter page, application. These objects are available to the Java codes included by the <code>zscript</code> element and the attributes for specifying event listeners. They are also available to EL expressions.
  
For example, <tt>self</tt> is an instance of <tt>org.zkoss.zk.ui.Component</tt> to represent the component being processing. In the following example, you could identify the component in an event listener by <tt>self</tt>.
+
For example, <code>self</code> is an instance of <javadoc type="interface">org.zkoss.zk.ui.Component</javadoc> to represent the component being processing. In the following example, you could identify the component in an event listener by <code>self</code>.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 10: Line 10:
 
</source>
 
</source>
 
   
 
   
Similarly, <tt>event</tt> is the current event being processed by an event listener. Thus, the above statement is equivalent to
+
Similarly, <code>event</code> is the current event being processed by an event listener. Thus, the above statement is equivalent to
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 17: Line 17:
  
  
== List of Implicit Objects ==
+
{{ZKDevelopersGuideHeadingToc}}
  
{| border="1"
 
! <center>Object Name</center>
 
! <center>Type</center>
 
! <center>Description</center>
 
 
|-
 
|  self
 
|  [http://www.zkoss.org/javadoc/3.5.1/zk/org/zkoss/zk/ui/Component.html <tt>org.zkoss.zk.ui.Component</tt>]
 
 
|The component itself.
 
 
|-
 
|  spaceOwner
 
|  [http://www.zkoss.org/javadoc/3.5.1/zk/org/zkoss/zk/ui/idspace.html <tt>org.zkoss.zk.ui.IdSpace</tt>]
 
 
|The space owner of this component. It is the same as <tt>self.spaceOwner</tt>.
 
 
|-
 
|  page
 
|  [http://www.zkoss.org/javadoc/3.5.1/zk/org/zkoss/zk/ui/page.html <tt>org.zkoss.zk.ui.Page</tt>]
 
 
|The page. It is the same as <tt>self.page</tt>.
 
 
|-
 
|  desktop
 
|  [http://www.zkoss.org/javadoc/3.5.1/zk/org/zkoss/zk/ui/desktop.html <tt>org.zkoss.zk.ui.Desktop</tt>]
 
 
|The desktop. It is the same as <tt>self.desktop</tt>.
 
 
|-
 
|  execution
 
|  [http://www.zkoss.org/javadoc/3.5.1/zk/org/zkoss/zk/ui/execution.html <tt>org.zkoss.zk.ui.Execution</tt>]
 
 
|The current execution.
 
 
|-
 
|  session
 
|  [http://www.zkoss.org/javadoc/3.5.1/zk/org/zkoss/zk/ui/session.html <tt>org.zkoss.zk.ui.Session</tt>]
 
 
|The session.
 
 
|-
 
|  application
 
|  [http://www.zkoss.org/javadoc/3.5.1/zk/org/zkoss/zk/ui/webapp.html <tt>org.zkoss.zk.ui.WebApp</tt>]
 
 
|The Web application.
 
 
|-
 
|  componentScope
 
|  <tt>java.util.Map</tt>
 
 
|A map of attributes defined in the component. It is the same as the <tt>getAttributes</tt> method in the <tt>org.zkoss.zk.ui.Component</tt> interface.
 
 
|-
 
|  spaceScope
 
|  <tt>java.util.Map</tt>
 
 
|A map of attributes defined in the ID space containing this component.
 
 
|-
 
|  pageScope
 
|  <tt>java.util.Map</tt>
 
 
|A map of attributes defined in the page. It is the same as the <tt>getAttributes</tt> method in the <tt>org.zkoss.zk.ui.Page</tt> interface.
 
 
|-
 
|  desktopScope
 
|  <tt>java.util.Map</tt>
 
 
|A map of attributes defined in the desktop. It is the same as the <tt>getAttributes</tt> method in the <tt>org.zkoss.zk.ui.Desktop</tt> interface.
 
 
|-
 
|  sessionScope
 
|  <tt>java.util.Map</tt>
 
 
|A map of attributes defined in the session. It is the same as the <tt>getAttributes</tt> method in the <tt>org.zkoss.zk.ui.Session</tt> interface.
 
 
|-
 
|  applicationScope
 
|  <tt>java.util.Map</tt>
 
 
|A map of attributes defined in the web application. It is the same as the <tt>getAttributes</tt> method in the <tt>org.zkoss.zk.ui.WebApp</tt> interface.
 
 
|-
 
|  requestScope
 
|  <tt>java.util.Map</tt>
 
 
|A map of attributes defined in the request. It is the same as the <tt>getAttributes</tt> method in the <tt>org.zkoss.zk.ui.Execution</tt> interface.
 
 
|-
 
|  param
 
|  <tt>java.util.Map</tt>
 
 
|A map of attributes passed by the query string. Please refer to section [[Work_with_HTML_Tags#Pass_Values_to_the_Included_Page|Pass Values to the Included Page]] for example.
 
 
|-
 
|  arg
 
|  <tt>java.util.Map</tt>
 
 
|The <tt>arg</tt> argument passed to the <tt>createComponents</tt> method in the <tt>org.zkoss.zk.ui.Executions</tt> class. It is never <tt>null</tt>.
 
 
It is the same as <tt>self.desktop.execution.arg</tt>.
 
 
Notice that <tt>arg</tt> is available only when creating the components for the included page (the first argument of <tt>createComponents</tt>). On the other hand, all events, including <tt>onCreate</tt>, are processed later. Thus, if you want to access <tt>arg</tt> in the <tt>onCreate</tt>'s listener, use the <tt>getArg</tt> method of the <tt>org.zkoss.zk.ui.event.CreateEvent</tt> class.
 
 
 
 
|-
 
|  each
 
|  <tt>java.lang.Object</tt>
 
 
|The current item of the collection being iterated, when ZK evaluates an iterative element. An iterative element is an element with the forEach attribute.
 
 
|-
 
|  forEachStatus
 
|  [http://www.zkoss.org/javadoc/3.5.1/zk/org/zkoss/zk/ui/util/foreachstatus.html <tt>org.zkoss.zk.ui.util.ForEachStatus</tt>]
 
 
|The status of an iteration. ZK exposes the information relative to the iteration taking place when evaluating the iterative element.
 
 
|-
 
|  event
 
|  [http://www.zkoss.org/javadoc/3.5.1/zk/org/zkoss/zk/ui/event/event.html <tt>org.zkoss.zk.ui.event.Event</tt>] or derived
 
 
|The current event. Available for the event listener only.
 
 
|}
 
 
 
Implicit objects provides many useful methods for web application. You can click the hyperlink to see the java doc of each implicit object.
 
 
== Information about Request and Execution ==
 
An execution of a client request (e.g., ServletRequest). When a request sent from a client, the server constructs a Execution  object to hold execution relevant info, and then serves the request through this execution.
 
 
A client request, e.g., HttpServletRequest, might consist of multiple ZK request (AuRequest). However, these ZK requests must target the same desktop of pages (Page).
 
 
The <tt>org.zkoss.zk.ui.Execution</tt> interface provides information about the current execution, such as the request parameters. To get the current execution, you could simply use the <tt>getCurrent</tt> method in the <tt>org.zkoss.zk.ui.Executions</tt> class. It's a static class.
 
 
The following example will show the server name.
 
<source lang="xml" >
 
<window>
 
<zscript>
 
String sn = Executions.getCurrent().getServerName();
 
</zscript>
 
The server is: ${sn}
 
</window>
 
</source>
 
 
==Life cycle of page, execution, desktop, session, application==
 
 
From small scope to big:
 
*page
 
: A single page.
 
*execution<ref><tt>execution</tt> in ZK is a wrapper of <tt>request</tt> in JSP </ref>
 
: Processing a request from client side. It can cross pages.
 
*desktop
 
: All pages serving the same URL, for one browser tab.
 
*session
 
: From user visit the web to user leave or timeout. All following pages are related to same session.
 
*application
 
: From the application service start to stop. It is cross session, cross users.
 
 
Note that, if you press "F5" to refresh the web page, it's a different desktop, but the same session.
 
 
==About xxxScope==
 
Each xxxScope is a map of attribute of certain component. You can use it as a temporary space to save and retrieve data. It's a mechanism to realize communication between pages, applications. You have to understand each component's life cycle, to decide which scope you should use. In most cases, you should use <tt>session</tt>.
 
<tt>setAttribute</tt>, and <tt>getAttribute</tt> are the api to set and get variable in certain xxxScope.
 
 
 
'''Note'''
 
<references/>
 
 
==See Also==
 
:For more information, please refer to [http://www.zkoss.org/doc/devref-single/index.html#id456270 developer's reference]. The concept of implicit object is much like [http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro7.html#70899 JSP].
 
:[[Most_used_api_for_Implicit_Objects_and_Static_Classes | Most used api for Implicit Objects and Static Classes]] -- includes Executions, Path, Clients
 
 
== Quiz ==
 
 
1.Find Out the api of implicit object, that can
 
 
*Creates components from the specified page definition
 
 
*Sets a bookmark to this desktop.
 
 
*Returns the Internet Protocol (IP) address of the interface on which the first request was received (and creates this session).
 
 
2.It's common to include another <tt>page</tt> inside current <tt>page</tt>, and it's a common need to pass variable between these 2 pages. Please refer to [[Work_with_HTML_Tags#The_include_Component | The include component]]. Use <tt>session.setAttribute</tt> and <tt>getAttribute</tt> to pass the value of  <tt>passed</tt> between following 2 zul pages.
 
 
helloworld.zul
 
<source lang="xml" >
 
<window>
 
    <label id="passed" value="Hello, World!"/>
 
    <include src="included.zul"/>   
 
</window>
 
</source>
 
 
And included.zul:
 
<source lang="xml" >
 
<window>
 
The value of passed is
 
</window>
 
</source>
 
  
 
{{ ZKDevelopersGuidePageFooter}}
 
{{ ZKDevelopersGuidePageFooter}}

Latest revision as of 10:36, 19 January 2022

Stop.png This documentation is for an older version of ZK. For the latest one, please click here.


Overview

For scripts embedded in a ZUML page, there are a set of implicit objects that enable developers to access components more efficiently. It's also a mechanism to pass data inter page, application. These objects are available to the Java codes included by the zscript element and the attributes for specifying event listeners. They are also available to EL expressions.

For example, self is an instance of Component to represent the component being processing. In the following example, you could identify the component in an event listener by self.

 <button label="Try" onClick="alert(self.label)"/>

Similarly, event is the current event being processed by an event listener. Thus, the above statement is equivalent to

<button label="Try" onClick="alert(event.target.label)"/>





Last Update : 2022/01/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.