Use Compiled Java Codes

From Documentation
Revision as of 10:24, 5 February 2024 by Hawk (talk | contribs) (remove empty version history (via JWB))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Use Compiled Java Codes

Not to Use zscript for Better Performance

It is convenient to use zscript in ZUML, but it comes with a price: slower performance. The degradation varies from one application to another. It is suggested to use zscript only for fast prototyping, POC, or small projects. For large websites, it is suggested to use ZK MVC/ZK MVVM instead. For example, with ZK MVC

1  <window apply="foo.MyComposer">
2 //omitted

You can handle all events and components in foo.MyComposer. By the use of auto-wiring, it is straightforward to handle events and components.

Event Handler Is zscript

In addition to the zscript element, the event handler declared in a ZUL page is also interpreted at the runtime. For example,

1   <button label="OK" onClick="doSomething()"/>

where doSomething() is interpreted as zscript. Thus, for better performance, they should be replaced too.

Turn off the use of zscript

Since 5.0.8

If you decide not to use zscript at all, you could turn on the disable-script configuration as follows, such that an exception will be thrown if zscript is used.


Use the deferred Attribute

If you still need to write zscript codes, you can specify the deferred attribute to defer the evaluation of zscript codes as follows.

1 <zscript deferred="true">
2 </zscript>

By specifying the deferred attribute, the zscript codes it contains will not be evaluated when ZK renders a page. It means that the interpreter won't be loaded when ZK renders a page. This saves memory and speeds up page rendering.

In the following example, the interpreter is loaded only when the button is clicked:

1 <window id="w">
2     <zscript deferred="true">
3      void addMore() {
4          new Label("More").setParent(w);
5      }
6     </zscript>
7     <button label="Add" onClick="addMore()"/>
8 </window>

The deferred Attribute and the onCreate Event

It is worth to notice that, if the onCreate event listener is written in zscript, the deferred option mentioned in the previous section becomes useless. It is because the onCreate event is sent when the page is loaded. In other words, all deferred zscript will be evaluated when the page is loaded if the onCreate event listener is written in zscript as shown below.

1 <window onCreate="init()">

Rather, it is better to rewrite it as

1 <window use="my.MyWindow">

Then, prepare as shown below.

1  package my;
2  public class MyWindow extends Window {
3      public void onCreate() { //to process the onCreate event
4  ...

If you prefer to do the initialization right after the component (and all its children) is created, you can implement the AfterCompose interface as shown below. Note: the afterCompose method of the AfterCompose interface is evaluated at the Component Creation phase, while the onCreate event is evaluated in the Event Processing Phase.

1  package my;
2  public class MyWindow extends Window implements org.zkoss.zk.ui.ext.AfterCompose {
3      public void afterCompose() { //to initialize the window
4  ...

Use the forward Attribute

To simplify the event flow, ZK components usually send the events to the component itself, rather than the parent or other targets. For example, when a user clicks a button, the onClick event is sent to the button. However, developers may need to forward the event to the window component by the use of the onClick event listener as follows.

<window id="w" onOK='alert("on OK")'>
    <button label="OK" onClick='Events.postEvent("onOK", w, null)'/>

As suggested in the previous sections, the performance can be improved by not using zscript at all. Thus, you can rewrite the above code snippet either with EventListener or by specifying the forward attribute as follows.

<window apply="foo.MyComposer">
    <button label="OK" forward="onOK"/>

Last Update : 2024/02/05

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