---
title: "ZK 事件"
author: "编程足球"
date: "2012-05-10T17:00:26Z"
tags: []
categories: ["Web前端"]
slug: "zk"
draft: false
lastmod: "2012-05-10T17:00:26Z"
id: 1522260
url: "https://lingf.iteye.com/blog/1522260"
---

1. <span style="color:red">ZK事件</span>

zul代码如下

```xml

<window title="ZK Essentials" mode="overlapped" border="normal" width="250px">
	      <label id="lbl"/>World !	      
	      <!-- lbl.setValue(self.getLabel); -->
	      <button label="Hello " onClick="lbl.value = self.label"/>
	      <button label="Good-bye " onClick="lbl.value = self.label"/>
	</window>

```


界面如下:
![](http://dl.iteye.com/upload/attachment/0067/8934/41b1d0bb-e8cf-34ca-b1a1-24b69fa04916.jpg)

生成的html主要代码如下:

```html

<div id="tIuP0-cave" class="z-window-overlapped-cnt">
   <span id="tIuP1" class="z-label"></span>
   <span id="tIuP2" class="z-label">World ! </span>
   <button id="tIuP3" class="z-button-os" type="button">Hello </button>
   <button id="tIuP4" class="z-button-os" type="button">Good-bye </button>
</div>

```


可以知道 ZK enhanced XML parser 会给每个控件加上控件ID


<span style="color:red">点击 Hello 按钮时候,浏览器发起http请求：</span>

![](http://dl.iteye.com/upload/attachment/0067/8950/5950d031-33dc-3443-900e-04b717517dda.jpg)

服务器响应:

![](http://dl.iteye.com/upload/attachment/0067/8957/d4bd5f34-54b8-3297-89da-8e1f80a18e8b.jpg)


<span style="color:red">2. ZK 事件绑定在java代码中</span>
 At line 1, we declared: <span style="color:red">apply=controller class name</span> so that the events fired within the window is all forwarded to
 the controller for event handling.

 At line 2, we need to give the "Hello" <span style="color:red">button an ID</span> so the controller would know which button fired during the
 onClick event.

1. 定义 apply

```xml

<window id="win" title="ZK Essentials" border="normal" width="250px"   apply="demo.zkoss.SampleCtrl>
      <button id="helloBtn" label="Hello"/>
</window>

```


2. 绑定  'event_name$component_id'

```java

package demo.zkoss;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Button;
import org.zkoss.zul.Window;
public class SampleCtrl extends GenericForwardComposer {
      Window win;
      public void onClick$helloBtn(){
            Button btn = new Button();
            btn.setLabel("World !");
            btn.setParent(win);
      }
}

```