这就是所谓的
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模式而建立的。
许多开发小组,在开发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,带着对象跳转页面的方法。