Implicit Objects
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)"/>
List of Implicit Objects
self | Component | The component itself. |
spaceOwner | IdSpace | The space owner of this component. It is the same as Owner. |
page | Page | The page. It is the same as self.page. |
desktop | Desktop | The desktop. It is the same as self.desktop. |
execution | Execution | The current execution. |
session | Session | The session. |
application | WebApp | The Web application. |
componentScope | java.util.Map | A map of attributes defined in the component. It is the same as the getAttributes method in the Component interface. |
spaceScope | java.util.Map | A map of attributes defined in the ID space containing this component. |
pageScope | java.util.Map | A map of attributes defined in the page. It is the same as the getAttributes method in the Page interface. |
desktopScope | java.util.Map | A map of attributes defined in the desktop. It is the same as the getAttributes method in the Desktop interface. |
sessionScope | java.util.Map | A map of attributes defined in the session. It is the same as the getAttributes method in the Session interface. |
applicationScope | java.util.Map | A map of attributes defined in the web application. It is the same as the getAttributes method in the WebApp interface. |
requestScope | java.util.Map | A map of attributes defined in the request. It is the same as the getAttributes method in the Execution interface. |
param | java.util.Map | A map of attributes passed by the query string. Please refer to section Pass Values to the Included Page for example. |
arg | java.util.Map | The arg argument passed to the createComponents method in the Executions class. It is never null.
It is the same as self.desktop.execution.arg. Notice that arg is available only when creating the components for the included page (the first argument of createComponents). On the other hand, all events, including onCreate, are processed later. Thus, if you want to access arg in the onCreate's listener, use the getArg method of the CreateEvent class.
|
each | java.lang.Object | 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 | ForEachStatus | The status of an iteration. ZK exposes the information relative to the iteration taking place when evaluating the iterative element. |
event | Event 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 Execution interface provides information about the current execution, such as the request parameters. To get the current execution, you could simply use the getCurrent method in the Executions class. It's a static class.
The following example will show the server name.
<window>
<zscript>
String sn = Executions.getCurrent().getServerName();
</zscript>
The server is: ${sn}
</window>
Life cycle of page, execution, desktop, session, application
From small scope to big:
- page
- A single page.
- execution[1]
- 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 session. setAttribute, and getAttribute are the api to set and get variable in certain xxxScope.
Note
- ↑ execution in ZK is a wrapper of request in JSP
See Also
- For more information, please refer to developer's reference. The concept of implicit object is much like JSP.
- 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 page inside current page, and it's a common need to pass variable between these 2 pages. Please refer to The include component. Use session.setAttribute and getAttribute to pass the value of passed between following 2 zul pages.
helloworld.zul
<window>
<label id="passed" value="Hello, World!"/>
<include src="included.zul"/>
</window>
And included.zul:
<window>
The value of passed is
</window>