
這就是所謂的
Model 2的說明圖。可以說,一切就這個圖完美的表示了,畢竟也不是什么難以理解的東西。詳細解釋,我沒書上說的好,也懶得說,就copy過來。
Designers looked at MVC and modified it to work within this new development paradigm. This work led to what is now popularly called “Model 2” (to distinguish it from the desktop-centric MVC). Model 2 doesn’t change the definition of MVC; it just casts it in terms of web development. In Model 2 for Java web applications, JavaBeans represent the model. Notice that this may include simple JavaBeans, Enterprise JavaBeans (EJBs), or JavaBeans that act as proxies for EJBs. The view is rendered with JSP, which makes sense because JSP is closely tied to HTML. The controller is a servlet, well suited to executing Java code. This plays to the strengths of servlets, utilizing the services of the servlet container for lifecycle and invocation without forcing servlets to generate mixed Java code and HTML.
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模式而建立的。
許多開發(fā)小組,在開發(fā)Java Web Application的時候,都是直接選擇在各種framework的基礎(chǔ)上進行開發(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就是大名鼎鼎的,也是我唯一用過的,當然就是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再開發(fā),對于學校的同學來說,是不現(xiàn)實的。二是實用性也不強,struts中的很多功能,我們這里并不需要。
結(jié)論是,咱自己設(shè)計了另一個基于model 2的framework,當然,稱為framework真不好意思。但是,想來想去,自己這套東西,真的有可以重用的東西,的確可以實現(xiàn)一些“genetic”的功能。暫且就認為是一種幼稚的framework吧。由于是給研究生院(graduate school)做的,就叫gs框架吧。雖然功能簡陋,但也有它弱智的能力,其由以下一些類和支持元素組成:
1. 一個基類servlet:GSServlet。類圖如下:

gs框架下的servlet都要繼承于它。
a) 子類要override以下的方法:
n validate,控制此servlet能被誰訪問的代碼。其實內(nèi)容就是初始化屬性validator。
n process,此servlet邏輯業(yè)務(wù)代碼,以前寫在doPost或doGet中的代碼。
b) 子類可以使用的方法:
n forwardErr,出錯跳轉(zhuǎn)方法,當然根據(jù)不同的出錯參數(shù),有不同的出錯顯示。
n forward,跳轉(zhuǎn)下一個頁面的方法。
n forwardSuccess,有了forward,此方法舍棄了。
n forwardWithObject,帶著對象跳轉(zhuǎn)頁面的方法。