前段時間,實驗室接了學校研究生院的MIS系統。
采用Delphi的CS和J2EE的BS結合的構架方式。數據庫用的是師兄最熟悉的Oracle。說道Oracle,對我來說,基本使用沒有問題了,只是數據建模我還沒有主導設計過,心里還是沒有底。
按照分配,我還是主導BS的設計,更具體的說就是完成一個Java Web Application,再具體一點就是搞搞jsp,servlet,tag,js,beans。呵呵,技術很簡單,以前也做過了。
但是,由于這回小組的Leader是自己,我的自由度更大了一些。能發揮什么呢,第一個靈感還是“模式”二字。那就用用吧。但肯定是需求是第一位的,沒有需要而亂用或硬用會制造無端的復雜度,讓產品遠離需求。所以,一定要根據實際需要來用模式。
除了我們實驗室外,由于這個項目的特殊性,必須要聯合別的老師的4個小組一起做。我們導師是大Leader,所以,BS和CS的設計規范都是我們定,一定要保證大框架的可集成性,公共資源的單一性。
說到Java Web Application的開發,以下這本書(Art of Java Web Development)不得不說。反正無論對錯,它是我的教科書。
想了解這本書詳細的信息,請點擊這里。
它從演化(evolution)的角度,一步一步的告訴你,Java Web Application是怎么一步一步演變過來的。說實話,我也就看了這書的前半部分。也就是主要的演變史,而后半部分的設計模式的高階應用的例子沒有“所謂的時間”看了。呵呵。
談到Java Web Application的開發,最基本的一個模式(跟“設計模式”的“模式”有一定的區別)就是MVC模式,而MVC最開始就是為了規范Desktop UI Application的開發而提出的。如果要跟Web Application結合的更緊一些。它就有另外一個形式,應此便有了Model 2的由來。
因此,下面我們經常說的Model 2的全稱應該是MVC Model 2。
這就是所謂的
Designers looked at MVC and modified it to work within this new development paradigm. This work led to what is now popularly called “Model
The typical Model 2 scenario is shown in figure 1.2. The user invokes a controller servlet (more about this design later). The servlet instantiates one or more JavaBeans that perform work. The servlet then adds the bean(s) to one of the JSP collections and forwards control to a JSP. The JSP extracts the JavaBeans and displays the results.
Model 2可以說是以下N多framework的原型。很多framework都是緊密基于這個model 2模式而建立的。
許多開發小組,在開發Java Web Application的時候,都是直接選擇在各種framework的基礎上進行開發的。下面列出一些常用的frameworks。
Framework |
Download from |
Description |
Struts |
http://jakarta.apache.org/struts |
A lightweight, open–source framework primarily designed for building Model 2 applications. |
Velocity |
http://jakarta.apache.org/velocity |
A Java-based template engine. Velocity permits anyone to use the simple yet powerful template language to reference objects defined in Java code. |
Tapestry |
http://jakarta.apache.org/tapestry |
A framework that is positioned primarily as an alternative to JavaServer Pages. It replaces the scripting and code generation of JSPs with a full-fledged component object model. |
WebWork |
http://sourceforge.net/projects/opensymphony |
A community project conducted using the open-source process, aimed at providing tools and a framework for building complex web sites in a short amount of time that are easy to understand and maintain. |
Turbine |
http://jakarta.apache.org/turbine |
A large, open-source, services-based framework for building extensive web applications such as e-commerce sites. |
而最最常用的framework就是大名鼎鼎的,也是我唯一用過的,當然就是struts啦。它的特點有:
n A controller servlet that dispatches requests to appropriate action classes provided by the application developer
n JSP custom tag libraries and associated support in the controller servlet that assists developers in creating interactive form-based applications
n Utility classes that support XML parsing, automatic population of JavaBeans properties based on the Java reflection APIs, and internationalization of prompts and messages
呵呵,最后我們項目沒有使用這里任何的一個framework,一是覺得成本高,5個小組都沒有什么接觸過的人,要搞懂一個framework再開發,對于學校的同學來說,是不現實的。二是實用性也不強,struts中的很多功能,我們這里并不需要。
結論是,咱自己設計了另一個基于model 2的framework,當然,稱為framework真不好意思。但是,想來想去,自己這套東西,真的有可以重用的東西,的確可以實現一些“genetic”的功能。暫且就認為是一種幼稚的framework吧。由于是給研究生院(graduate school)做的,就叫gs框架吧。雖然功能簡陋,但也有它弱智的能力,其由以下一些類和支持元素組成:
1. 一個基類servlet:GSServlet。類圖如下:
gs框架下的servlet都要繼承于它。
a) 子類要override以下的方法:
n validate,控制此servlet能被誰訪問的代碼。其實內容就是初始化屬性validator。
n process,此servlet邏輯業務代碼,以前寫在doPost或doGet中的代碼。
b) 子類可以使用的方法:
n forwardErr,出錯跳轉方法,當然根據不同的出錯參數,有不同的出錯顯示。
n forward,跳轉下一個頁面的方法。
n forwardSuccess,有了forward,此方法舍棄了。
n forwardWithObject,帶著對象跳轉頁面的方法。
2. 一個對用戶驗證的框架(如下圖),每個servlet都會用此驗證框架來指定可訪問用戶的身份。
這個框架可重用的部分為接口Validator、抽象類Role和兩個關系類OrRelation,AndRelation。
而繼承于Role的各個具體角色則要根據不同的項目的實際情況自行定義,gs項目中暫時只有三種角色:游客,學生,教師。對于這個的使用,后面會說到的。明眼人一看就可以知道這里用了裝飾模式,我自豪啊。
3. JSTL,這里使用了JSTL作為頁面的標準標記。
在頁面中的條件判斷和迭代,就都靠它了。相對于自定義標記,使用JSTL作為jsp標記,我個人認為是性價比非常好的一種選擇,畢竟學習自定義標記的開發要一定的時間,而且一但使用,量一定很大。
4. tipErr.jsp,顯示出錯信息頁面。
出錯提示對于一個framework肯定是必要的,既然GSServlet中有通用的跳轉出錯頁面的方法,那也就把這個顯示出錯信息頁面當作框架的一部分吧,不妥之處請見諒。
由上面的四點構成了本項目的一個簡易framework。
下面是基于這種framework開發的一個servlet例子。其功能是為教師和學生提供成績查詢。類圖如下:
框架代碼:
首先,必須Override父類的process方法,可見,本servlet的業務邏輯都在這里。

2



3

4

5

6

7

8



9

10



11

12



13

14



15

16



17

18

19

20

接著,必須Override父類的validate方法,實現對本servlet的訪問控制。



2

3

4

5

再者,有多少子邏輯功能,就寫多少個下面的業務方法。父類GSServlet的一些跳轉方法這里就可以用了。


2

3

4

5

6

7

8

9



10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26



27

28

29

30



31

32



33

34

35

36



37



38

39



40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55


56

57

58

59

60

61

62

63



64

65

66

67

68

顯示的jsp頁面就用JSTL把forward過來的東西顯示出來,一個業務過程就完成了。
這個簡單的framework在本項目中基本符合使用要求,特別是別的小組的同學不需要花太多的時間就能很好的使用,希望以后實驗室能夠考慮重用這個東西,呵呵。
MARCO ZHANG 2006年2月10日5:10:08
--“design with the think of sharing objects”,Flyweight模式應用
--“make the method become dynamic”,“洋蔥”模式應用
--“Ignore how they were created”,工廠方法模式應用