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類似吧。

 

(四)運行結果

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM