前段時(shí)間,實(shí)驗(yàn)室接了學(xué)校研究生院的MIS系統(tǒng)。
采用Delphi的CS和J2EE的BS結(jié)合的構(gòu)架方式。數(shù)據(jù)庫用的是師兄最熟悉的Oracle。說道Oracle,對(duì)我來說,基本使用沒有問題了,只是數(shù)據(jù)建模我還沒有主導(dǎo)設(shè)計(jì)過,心里還是沒有底。
按照分配,我還是主導(dǎo)BS的設(shè)計(jì),更具體的說就是完成一個(gè)Java Web Application,再具體一點(diǎn)就是搞搞jsp,servlet,tag,js,beans。呵呵,技術(shù)很簡單,以前也做過了。
但是,由于這回小組的Leader是自己,我的自由度更大了一些。能發(fā)揮什么呢,第一個(gè)靈感還是“模式”二字。那就用用吧。但肯定是需求是第一位的,沒有需要而亂用或硬用會(huì)制造無端的復(fù)雜度,讓產(chǎn)品遠(yuǎn)離需求。所以,一定要根據(jù)實(shí)際需要來用模式。
除了我們實(shí)驗(yàn)室外,由于這個(gè)項(xiàng)目的特殊性,必須要聯(lián)合別的老師的4個(gè)小組一起做。我們導(dǎo)師是大Leader,所以,BS和CS的設(shè)計(jì)規(guī)范都是我們定,一定要保證大框架的可集成性,公共資源的單一性。
說到Java Web Application的開發(fā),以下這本書(Art of Java Web Development)不得不說。反正無論對(duì)錯(cuò),它是我的教科書。
想了解這本書詳細(xì)的信息,請(qǐng)點(diǎn)擊這里。
它從演化(evolution)的角度,一步一步的告訴你,Java Web Application是怎么一步一步演變過來的。說實(shí)話,我也就看了這書的前半部分。也就是主要的演變史,而后半部分的設(shè)計(jì)模式的高階應(yīng)用的例子沒有“所謂的時(shí)間”看了。呵呵。
談到Java Web Application的開發(fā),最基本的一個(gè)模式(跟“設(shè)計(jì)模式”的“模式”有一定的區(qū)別)就是MVC模式,而MVC最開始就是為了規(guī)范Desktop UI Application的開發(fā)而提出的。如果要跟Web Application結(jié)合的更緊一些。它就有另外一個(gè)形式,應(yīng)此便有了Model 2的由來。
因此,下面我們經(jīng)常說的Model 2的全稱應(yīng)該是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都是緊密基于這個(gè)model 2模式而建立的。
許多開發(fā)小組,在開發(fā)Java Web Application的時(shí)候,都是直接選擇在各種framework的基礎(chǔ)上進(jìn)行開發(fā)的。下面列出一些常用的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就是大名鼎鼎的,也是我唯一用過的,當(dāng)然就是struts啦。它的特點(diǎn)有:
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
呵呵,最后我們項(xiàng)目沒有使用這里任何的一個(gè)framework,一是覺得成本高,5個(gè)小組都沒有什么接觸過的人,要搞懂一個(gè)framework再開發(fā),對(duì)于學(xué)校的同學(xué)來說,是不現(xiàn)實(shí)的。二是實(shí)用性也不強(qiáng),struts中的很多功能,我們這里并不需要。
結(jié)論是,咱自己設(shè)計(jì)了另一個(gè)基于model 2的framework,當(dāng)然,稱為framework真不好意思。但是,想來想去,自己這套東西,真的有可以重用的東西,的確可以實(shí)現(xiàn)一些“genetic”的功能。暫且就認(rèn)為是一種幼稚的framework吧。由于是給研究生院(graduate school)做的,就叫gs框架吧。雖然功能簡陋,但也有它弱智的能力,其由以下一些類和支持元素組成:
1. 一個(gè)基類servlet:GSServlet。類圖如下:
gs框架下的servlet都要繼承于它。
a) 子類要override以下的方法:
n validate,控制此servlet能被誰訪問的代碼。其實(shí)內(nèi)容就是初始化屬性validator。
n process,此servlet邏輯業(yè)務(wù)代碼,以前寫在doPost或doGet中的代碼。
b) 子類可以使用的方法:
n forwardErr,出錯(cuò)跳轉(zhuǎn)方法,當(dāng)然根據(jù)不同的出錯(cuò)參數(shù),有不同的出錯(cuò)顯示。
n forward,跳轉(zhuǎn)下一個(gè)頁面的方法。
n forwardSuccess,有了forward,此方法舍棄了。
n forwardWithObject,帶著對(duì)象跳轉(zhuǎn)頁面的方法。
2. 一個(gè)對(duì)用戶驗(yàn)證的框架(如下圖),每個(gè)servlet都會(huì)用此驗(yàn)證框架來指定可訪問用戶的身份。
這個(gè)框架可重用的部分為接口Validator、抽象類Role和兩個(gè)關(guān)系類OrRelation,AndRelation。
而繼承于Role的各個(gè)具體角色則要根據(jù)不同的項(xiàng)目的實(shí)際情況自行定義,gs項(xiàng)目中暫時(shí)只有三種角色:游客,學(xué)生,教師。對(duì)于這個(gè)的使用,后面會(huì)說到的。明眼人一看就可以知道這里用了裝飾模式,我自豪啊。
3. JSTL,這里使用了JSTL作為頁面的標(biāo)準(zhǔn)標(biāo)記。
在頁面中的條件判斷和迭代,就都靠它了。相對(duì)于自定義標(biāo)記,使用JSTL作為jsp標(biāo)記,我個(gè)人認(rèn)為是性價(jià)比非常好的一種選擇,畢竟學(xué)習(xí)自定義標(biāo)記的開發(fā)要一定的時(shí)間,而且一但使用,量一定很大。
4. tipErr.jsp,顯示出錯(cuò)信息頁面。
出錯(cuò)提示對(duì)于一個(gè)framework肯定是必要的,既然GSServlet中有通用的跳轉(zhuǎn)出錯(cuò)頁面的方法,那也就把這個(gè)顯示出錯(cuò)信息頁面當(dāng)作框架的一部分吧,不妥之處請(qǐng)見諒。
由上面的四點(diǎn)構(gòu)成了本項(xiàng)目的一個(gè)簡易framework。
下面是基于這種framework開發(fā)的一個(gè)servlet例子。其功能是為教師和學(xué)生提供成績查詢。類圖如下:
框架代碼:
首先,必須Override父類的process方法,可見,本servlet的業(yè)務(wù)邏輯都在這里。

2



3

4

5

6

7

8



9

10



11

12



13

14



15

16



17

18

19

20

接著,必須Override父類的validate方法,實(shí)現(xiàn)對(duì)本servlet的訪問控制。



2

3

4

5

再者,有多少子邏輯功能,就寫多少個(gè)下面的業(yè)務(wù)方法。父類GSServlet的一些跳轉(zhuǎn)方法這里就可以用了。


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過來的東西顯示出來,一個(gè)業(yè)務(wù)過程就完成了。
這個(gè)簡單的framework在本項(xiàng)目中基本符合使用要求,特別是別的小組的同學(xué)不需要花太多的時(shí)間就能很好的使用,希望以后實(shí)驗(yàn)室能夠考慮重用這個(gè)東西,呵呵。
MARCO ZHANG 2006年2月10日5:10:08
--“design with the think of sharing objects”,F(xiàn)lyweight模式應(yīng)用
--“make the method become dynamic”,“洋蔥”模式應(yīng)用
--“Ignore how they were created”,工廠方法模式應(yīng)用