ZK Framework(三、MVC模式)

官网例子:http://books.zkoss.org/wiki/ZK_Getting_Started/Get_ZK_Up_and_Running_with_MVC

(一)业务流程

例子介绍ZK采用的MVC设计模式来实现Web Project.业务流程如图:

        (来自官网例子)

  ①通过关键字搜索小车信息,显示结果;

  ②点击结果中的某一条记录,显示这条记录的详细信息。

 

(二)工程

  1、新建工程:warmup;工程最终结构如图:

这里只分析ZK的searchMvc.zul,SearchCotroller,其他的请自行脑补。

工程下载地址:http://www.cnblogs.com/jrsmith/admin/Files.aspx

warmup.rar

1、searchMvc.zul

分四部分的组件:最大层的window,顶层的hbox,中间的listbox,下面的hbox;

1.1 window的apply指定了后台哪个Controller类来处理它。

1.2 顶层hbox:定义了textbox和button组件,还用id,id是为了后台的Controller类对这个id对应的组件进行处理。

1.3 中间listbox:类似HTML里的table,tr,td; 或者学过ExtJs的,可以认为跟grid更相似。

  listhead:定义column的名称

  listitem:定义每个单元格里的内容值

  template:说明单元格的内容值是从model里遍历取出来

1.4 下面hbox:定义了imgage和vbox,都是为了将后台的数据render到相应位置。

 1 <window title="Search" width="600px" border="normal"
 2     apply="com.core.controller.SearchController">
 3     <hbox align="center">
 4         Keyword:
 5         <textbox id="keywordBox" />
 6         <button id="searchButton" label="Search" image="/img/search.png" />
 7     </hbox>
 8     <listbox id="carListbox" height="160px" emptyMessage="No car found in the result">
 9         <listhead>
10             <listheader label="Model" />
11             <listheader label="Make" />
12             <listheader label="Price" width="20%"/>
13         </listhead>
14         <template name="model">
15             <listitem>
16                 <listcell label="${each.model}"></listcell>
17                 <listcell label="${each.make}"></listcell>
18                 <listcell>$<label value="${each.price}" /></listcell>
19             </listitem>
20         </template>
21     </listbox>
22     <hbox style="margin-top:20px">
23         <image id="previewImage" width="250px" />
24         <vbox>
25             <label id="modelLabel" />
26             <label id="makeLabel" />
27             <label id="priceLabel" />
28             <label id="descriptionLabel" />
29         </vbox>
30     </hbox>
31 </window>

2. SearchCotroller.java

2.1 继承ZK的SelectorComposer<Component>

2.2 全局变量名称与searchMvc.zul里的组件id一一对应,且要在变量上面加上@Wire注解,这样的当从View转到Controller是,ZK会自动将View里面的组件以Object的形式注入到Controller里,以供操作。

2.3 search()

  2.3.1 @Listen("onClick = #searchButton"):监听View里面id="searchButton"组件的onClick事件;

   2.3.2 carListbox.setModel(new ListModelList<Car>(result)):将查询结果List转换成ZK里的ListModelList形式,然后set进listbox的model.

2.4 showDetail()

  与上面的类似,这里不详述了。(累死了)

 1 package com.core.controller;
 2 
 3 
 4 import java.util.List;
 5 
 6 import org.zkoss.zk.ui.Component;
 7 import org.zkoss.zk.ui.select.SelectorComposer;
 8 import org.zkoss.zk.ui.select.annotation.*;
 9 import org.zkoss.zul.*;
10 
11 import com.core.model.Car;
12 import com.core.service.CarService;
13 import com.core.serviceImpl.CarServiceImpl;
14 
15 public class SearchController extends SelectorComposer<Component> {
16 
17     private static final long serialVersionUID = 1L;
18     
19     @Wire
20     private Textbox keywordBox;
21     @Wire
22     private Listbox carListbox;
23     @Wire
24     private Label modelLabel;
25     @Wire
26     private Label makeLabel;
27     @Wire
28     private Label priceLabel;
29     @Wire
30     private Label descriptionLabel;
31     @Wire
32     private Image previewImage;
33     
34     
35     private CarService carService = new CarServiceImpl();
36     
37     @Listen("onClick = #searchButton")
38     public void search(){
39         String keyword = keywordBox.getValue();
40         List<Car> result = carService.search(keyword);
41         carListbox.setModel(new ListModelList<Car>(result));
42     }
43     
44     @Listen("onSelect = #carListbox")
45     public void showDetail(){
46         Car selected = carListbox.getSelectedItem().getValue();
47         previewImage.setSrc(selected.getPreview());
48         modelLabel.setValue(selected.getModel());
49         makeLabel.setValue(selected.getMake());
50         priceLabel.setValue(selected.getPrice().toString());
51         descriptionLabel.setValue(selected.getDescription());
52     }
53 }

 

 

 (三)ZK MVC

              (来自官网例子)

对应上面工程结构,

  1、View指searchMvc.zul

  2、Controller指com.core.controller

  3、Model指从com.core.model转换成ZK Data

本人觉得这跟我们学的Struts的MVC类似吧。

 

(四)运行结果

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2013-06-01 22:14  黄辉杰  阅读(3229)  评论(0编辑  收藏  举报