Life Cycle Listener"

From Documentation
(Created page with '{{ZKDevelopersReferencePageHeader}} __TOC__ You could have some custom initialization and cleanup when an application, a session, a desktop or an execution is instantiated or …')
 
m
 
(9 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
__TOC__
 
__TOC__
 
   
 
   
You could have some custom initialization and cleanup when an application, a session, a desktop or an execution is instantiated or about to being destroyed.
+
You could have some custom initialization and cleanup when an application, a session, a desktop or an execution is instantiated or about to be destroyed.
  
=Version History=
+
There are two steps:
{{LastUpdated}}
+
#Implements the corresponding interface. For example, <javadoc type="interface">org.zkoss.zk.ui.util.WebAppInit</javadoc> for application's initialization
{| border='1px' | width="100%"
+
#Register it in <code>WEB-INF/zk.xml</code>, or in Java.
! Version !! Date !! Content
+
 
 +
=Interfaces=
 +
{| class='wikitable' | width="100%"
 +
! Task!! Interface
 +
|-
 +
| Application Init
 +
| <javadoc type="interface">org.zkoss.zk.ui.util.WebAppInit</javadoc>
 +
|-
 +
| Application Cleanup
 +
| <javadoc type="interface">org.zkoss.zk.ui.util.WebAppCleanup</javadoc>
 +
|-
 +
| Session Init
 +
| <javadoc type="interface">org.zkoss.zk.ui.util.SessionInit</javadoc>
 +
|-
 +
| Session Cleanup
 +
| <javadoc type="interface">org.zkoss.zk.ui.util.SessionCleanup</javadoc>
 +
|-
 +
| Desktop Init
 +
| <javadoc type="interface">org.zkoss.zk.ui.util.DesktopInit</javadoc>
 +
|-
 +
| Desktop Cleanup
 +
| <javadoc type="interface">org.zkoss.zk.ui.util.DesktopCleanup</javadoc>
 +
|-
 +
| Execution Init
 +
| <javadoc type="interface">org.zkoss.zk.ui.util.ExecutionInit</javadoc>
 
|-
 
|-
| &nbsp;
+
| Execution Cleanup
| &nbsp;
+
| <javadoc type="interface">org.zkoss.zk.ui.util.ExecutionCleanup</javadoc>
| &nbsp;
 
 
|}
 
|}
 +
 +
Notice that ZK will instantiate an object from the class you registered for each callback. For example, an object is instantiated to invoke <javadoc method="init(org.zkoss.zk.ui.Desktop, java.lnag.Object)" type="interface">org.zkoss.zk.ui.util.DesktopInit</javadoc>, and another object instantiated to invoke <javadoc method="cleanup(org.zkoss.zk.ui.Desktop)" type="interface">org.zkoss.zk.ui.util.DesktopCleanup</javadoc>, even if you register a class that implements both <javadoc type="interface">org.zkoss.zk.ui.util.DesktopInit</javadoc> and <javadoc type="interface">org.zkoss.zk.ui.util.DesktopCleanup</javadoc>.
 +
 +
If you have something that is initialized in the init callback and have to clean it up in the cleanup callback, you cannot store it as a data member. Rather, you have to maintain it by yourself, such as storing it in the desktop's attributes (<javadoc type="interface" method="setAttribute(java.lang.String, java.lang.Object)">org.zkoss.zk.ui.Desktop</javadoc>), session's attributes or application's attributes.
 +
 +
=Registration=
 +
The registration in <code>WEB-INF/zk.xml</code> is the same, no matter what interface you implement:
 +
 +
<source lang="xml">
 +
<listener>
 +
    <listener-class>my.MyImplementation</listener-class>
 +
</listener>
 +
</source>
 +
 +
The registration in Java is done by <javadoc method="addListener(java.lang.Class)">org.zkoss.zk.ui.util.Configuration</javadoc>.
 +
 +
<source lang="java">
 +
webapp.getConfiguration().addListener(my.MyImplementation.class);
 +
</source>
 +
 +
  
 
{{ZKDevelopersReferencePageFooter}}
 
{{ZKDevelopersReferencePageFooter}}

Latest revision as of 09:26, 2 February 2024


Life Cycle Listener


You could have some custom initialization and cleanup when an application, a session, a desktop or an execution is instantiated or about to be destroyed.

There are two steps:

  1. Implements the corresponding interface. For example, WebAppInit for application's initialization
  2. Register it in WEB-INF/zk.xml, or in Java.

Interfaces

Task Interface
Application Init WebAppInit
Application Cleanup WebAppCleanup
Session Init SessionInit
Session Cleanup SessionCleanup
Desktop Init DesktopInit
Desktop Cleanup DesktopCleanup
Execution Init ExecutionInit
Execution Cleanup ExecutionCleanup

Notice that ZK will instantiate an object from the class you registered for each callback. For example, an object is instantiated to invoke DesktopInit.init(Desktop, Object), and another object instantiated to invoke DesktopCleanup.cleanup(Desktop), even if you register a class that implements both DesktopInit and DesktopCleanup.

If you have something that is initialized in the init callback and have to clean it up in the cleanup callback, you cannot store it as a data member. Rather, you have to maintain it by yourself, such as storing it in the desktop's attributes (Desktop.setAttribute(String, Object)), session's attributes or application's attributes.

Registration

The registration in WEB-INF/zk.xml is the same, no matter what interface you implement:

<listener>
    <listener-class>my.MyImplementation</listener-class>
</listener>

The registration in Java is done by Configuration.addListener(Class).

webapp.getConfiguration().addListener(my.MyImplementation.class);




Last Update : 2024/02/02

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