📄 l5.html
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="description" content="Java,JDBC,EJB,Open Source,jdk,rmi">
<meta name="Keywords"
content="Java, servlets, Java servlet, Javascript, ActiveX, VRML,
applet, applets, directory, news, jdbc, applications,
Java applications, Java developer, Java development, developer,
classes, Jars.com, Jars, intranet, Java applet, Javabeans,
Java products, JDK, Java development kit, java development environment, JIT,
JavaPlan, enterprise tools, JVM, Java Virtual Machine, Java resources,
SUN, CGI, Perl, database, network, html,
xml, dhtml, rating, ratings, review, jars, cgi, programming,
software review, software rating">
<title>csdn_JSP 构 架--2种方式:Model I和Model II</title>
<style>
.news { BACKGROUND: #007cd3; font-family: "宋体"; font-size: 9pt }
.t { font-family: "宋体"; font-size: 9pt }
.t1 { color:#007cd3; font-family: "宋体"; font-size: 9pt }
.white { font-family: "宋体"; font-size: 9pt;color:#FFFFFF }
.red { font-family: "宋体"; font-size: 9pt;color:#FF0000 }
A:visited {color:#0000FF}
A:hover {color: #ff6666; text-decoration: none}
.text {font-size: 12px; line-height: 160%; font-family: "宋体"}
.text1 {color:#000000; font-size: 12px; line-height: 130%; font-family: "宋体"; text-decoration: none}
.text1:visited {color:#000000}
.text1:hover {color: #000000}
.text2 {color:#000000; font-size: 12px; line-height: 130%; font-family: "宋体"; text-decoration: none}
.text2:visited {color:#000000}
.text2:hover {color: #000000}
.text3 {font-size: 12px; line-height: 100%; font-family: "宋体"; text-decoration: none}
.large {font-size: 14.8px; line-height: 130%}
</style>
</head>
<body
<center>
<tr>
<td WIDTH="100%" VALIGN="TOP">
<tr>
<td WIDTH="100%" CLASS="white"></td>
</tr>
<tr>
<td WIDTH="50%" bordercolor="#FFFFFF" CLASS="t1" bgcolor="#F0F0F0" align="center" nowrap>JSP 构 架--2种方式:Model I和Model II
</td>
</tr>
<tr>
<p><td WIDTH="50%" bordercolor="#FFFFFF" CLASS="t1" bgcolor="#F0F0F0" align="center" nowrap>作者:Lance Lavandowska   编译:blueski</td></p>
</tr>
<tr>
<td WIDTH="100%" bordercolor="#FFFFFF" CLASS="t" bgcolor="#F0F0F0" colspan="2">
<tr>
<td colspan="2" height="5"></td>
</tr>
<tr>
<td width="21%" style="border-right: 1px solid red" valign="top"><script
language="javascript" src="translateindex.js"></script> </td>
<td width="79%" valign="top">
</center>
<p>
<p> <p>如果你经常去Servlet或JSP的新闻组或者邮件列表,那么一定会看到不少关于Model
I 和Model II
方法的讨论。究竟采用哪一种,这取决于你的个人喜好、团队工作策略以及是否采用正统的OOP。 <br>
<br>
简单地说,Model I将事务逻辑(business logic)和表示代码(presentation code)融合在一起(如在HTML中);Model II则提倡最大限度地将所有的代码放到内容表示之外。<p>
<br>
<font><b>
Model I: 简单的单层次应用</b></font><p>如果是在一个人人都精通Java和HTML的环境中,或者你独自做着所有的工作,假如每个人都有清晰的编程结构和思路,那么这种方法会很有效,不过这样的假设不在本文讨论范围之内。这种方法的第一个优点是如果你的应用改变了,你只需维护一个文件。而最大的缺陷是可读性!除非十分小心,否则你的HTML和Java代码会相互混杂,从而难以维护。 <br>
<br>
在下面这个例子中,我们将增加一个 TimeZone
元素,从而使它变成JSP文件,它会返回基于时间的所期待的TimeZone。如果没有提交 TimeZone,那么缺省的是服务器的缺省时间。<br>
<br>
======================================================================<br>
<xml version="1.0" ?><br>
<H1>Time JSP</H1><br>
<jsp:scriptlet><br>
//the parameter "zone" shall be equal to a number between 0 and 24 (inclusive)<br>
TimeZone timeZone = TimeZone.getDefault(); //returns the default TimeZone for the server<br>
if (request.getParameterValues("zone") != null)<br>
{<br>
String timeZoneArg = request.getParameterValues("zone")[0];<br>
timeZone = TimeZone.getTimeZone("GMT+" + timeZoneArg + ":00"); <br>
// gets a TimeZone. For this example we're just going to assume <br>
// its a positive argument, not a negative one.<br>
}<br>
//since we're basing our time from GMT, we'll set our Locale to Brittania, and get a Calendar.<br>
Calendar myCalendar = Calendar.getInstance(timeZone, Locale.UK);<br>
</jsp:scriptlet><br>
<%= myCalendar.get(Calendar.HOUR_OF_DAY) %>:<br>
<%= myCalendar.get(Calendar.MINUTE) %>:<br>
<%= myCalendar.get(Calendar.SECOND) %><br>
======================================================================<br>
相应地,数据也可以从JavaBean取得并加以显示。在下一个例子中我们就可以看到。 <br>
<p><b><font>
Model II: 重定向请求(Redirecting Requests)</font></b><p><br>
在一个团队开发环境中,有些是HTML设计者,另一些则是Java程序员,这时这一方法显得非常重要。Java程序员可以集中精力创建可重用代码,而HTML设计师可以集中精力于内容表示,彼此相对对立,可以分别动态地修改自己的内容,只要总体的输入输出不变。 <br>
<br>
现在我们可以使用Model II来表示Model I的那个例子。这一方法遵循了Model-View-Controller (MVC)
范例 (cite Design Patterns book)。
在这个例子中,我们只有一个类(页或者servlet) 处理请求(Controller),取得TimeZone,设置所有用于表示的变量,并将控制传递到表示页(View)。作为如此简单的应用,可以没有
"Model"。 <br>
<br>
<b>
Controller: timeByZone.jsp</b><p><br>
controller可以是一个servlet或一个JSP页。我推荐使用JSP,因为这样我不必担心每当我做修改时要对类重新编译,但是,你将因此失去granularity(颗粒性),以后要扩展该类也比较困难。<p>
<br>
======================================================================<br>
<xml version="1.0" ?><br>
<!--Worker Class, nobody should see me--><br>
<jsp:scriptlet><br>
//the parameter "zone" shall be equal to a number between 0 and 24 (inclusive)<br>
TimeZone timeZone = TimeZone.getDefault(); //returns the default TimeZone for the server<br>
if (request.getParameterValues("zone") != null)<br>
{<br>
String timeZoneArg = request.getParameterValues("zone")[0];<br>
timeZone = TimeZone.getTimeZone("GMT+" + timeZoneArg + ":00"); <br>
// gets a TimeZone. For this example we're just going to assume <br>
// its a positive argument, not a negative one.<br>
}<br>
TimeBean timeBean = new TimeBean();<br>
timeBean.setHours = myCalendar.get(Calendar.HOUR_OF_DAY);<br>
timeBean.setMinutes = myCalendar.get(Calendar.MINUTE);<br>
timeBean.setSeconds = myCalendar.get(Calendar.SECOND);<br>
HttpSession mySession = request.getSession();<br>
mySession.putValue("tempTimeBean", timeBean);<br>
<br>
</jsp:scriptlet><br>
<jsp:forward page="displayTime.jsp" /><br>
======================================================================<br>
<p><b>
View: displayTime.jsp<br>
</b><p>同样地,这个view既可以是一个servlet也可以是一个jsp文件。这里我们从Session中取得并显示它的值。实际上我们会将这做两次,来示范Bean是如何被使用的。
<br>
<br>
======================================================================<br>
<xml version="1.0" ?><br>
<H1>Time JSP</H1><br>
<jsp:useBean class="TimeBean" id="tempTimeBean" scope="session" /> <br>
<jsp:getProperty name="tempTimeBean" property="hours">:<br>
<jsp:getProperty name="tempTimeBean" property="minutes">:<br>
<jsp:getProperty name="tempTimeBean" property="seconds"><br>
<!-- these would have printed "null" if tempTimeBean was not instantiated by timeByZone.jsp --><br>
<br>
<jsp:scriptlet><br>
HttpSession mySession = request.getSession();<br>
TimeBean timeBean = mySession.getValue("tempTimeBean");<br>
if (timeBean != null)<br>
{ // check to make sure its not null, to avoid NullPointerExceptions<br>
out.print(timeBean.getHours());<br>
out.print(":");<br>
out.print(timeBean.getMinutes());<br>
out.print(":");<br>
out.print(timeBean.getSeconds());<br>
}<br>
else<br>
{<br>
out.println("Press your Back button and select a TimeZone");<br>
}<br>
</jsp:scriptlet><br>
======================================================================<br>
第二种方法(在内部使用了代码)可能有些笨重,但允许开发者确保输出不至于很糟糕(例如"null:null:null null"),假定Session bean还没有被实例化以及没有进行值的设置。
这种情况发生在客户端直接调用了View页。问题是使用脚本scriptlets可以允许更强的控制。如果你确信你可以控制url存取,那么bean方法当然更适合于开发,并使
View页更方便于HTML设计者的协同工作。<p> <br>
上面的是"传统的" Model II设计。所有的变量都包装了并放在Session对象中。这有2个不足:<p> 1)
如果客户端拒绝参与的话,Session是不可得到的。<p> 2)
除非Session变量被显式地移走,否则它回一直存在,直到Session被破坏或过期。 <br>
<br>
第一种案例很可能发生在这样的场合,即使用了cookies作为声明的结构(mechanism)而开发者没有能够提供声明的结构的替代表单(form),即URL改写。 <br>
<br>
第二个案例甚至更为严重,因为它可能引起很大的内存消耗,如果Sessions被定义为保存比标准存留时间更长的话((标准存留时间是30分钟)。即使是30分钟的Session,这种Model也可能在大的应用中引起灾难性的内存泄露。为什么呢?在Session对象内部设置的对象被实例化了,并且在Session终止以前一直没有被移去。因为它们仍然有关联references(Session对象)
指向它们,所以无法被垃圾收集(garbage-collected)。在Model II
模型中,很多对象被放到Session中(要么直接地,要么通过JavaBean)。随着Session的进行,更多的页被存取,内存使用会增加并持续下去直到客户端终止了Session或者Session过期。要一直等到Session变得非法,放在那的对象才能被垃圾收集,而那些损失的内存本可以用于任何其它的用途。. <br>
<br>
改进的方法之一是将Beans或者其它变量放到Request对象中去,并使用RequestDispatcher.include()而不是RequestDispatcher.forward()。这样做以后,View
页具有和Controller一样的存取请求的对象。传统的Model II设计的不足可以被排除。<br>
<br>
一个最后的评注:尽管有如上所述,我个人仍有些不喜欢Model II
的范例,如果它用通常方法开发的话。
客户端被引送到某一个地址,然后又被转向到另一个不同的类,我不喜欢创建这样的系统。基于这样的原因,我修改了设计,使它变成了以下的样子: <br>
<br>
<b>
Controller: timeByZone2.jsp</b><p><br>
和前面一样,controller使用Request值来取得必要的数据,并且将数据放到请求的对象中去。这回的区别是View页将使用RequestDispatcher.include()来调用Controller。在这种方法中,客户端再也不做重定向,请求不是“链接chained”的。相当于class/jsp请求了另一方来为它做一些工作,然后继续。<br>
<br>
======================================================================<br>
<xml version="1.0" ?><br>
<!--Worker Class, nobody should see me--><br>
<jsp:scriptlet><br>
//the parameter "zone" shall be equal to a number between 0 and 24 (inclusive)<br>
TimeZone timeZone = TimeZone.getDefault(); //returns the default TimeZone for the server<br>
if (request.getParameterValues("zone") != null)<br>
{<br>
String timeZoneArg = request.getParameterValues("zone")[0];<br>
timeZone = TimeZone.getTimeZone("GMT+" + timeZoneArg + ":00"); <br>
// gets a TimeZone. For this example we're just going to assume <br>
// its a positive argument, not a negative one.<br>
}<br>
TimeBean timeBean = new TimeBean();<br>
timeBean.setHours = myCalendar.get(Calendar.HOUR_OF_DAY);<br>
timeBean.setMinutes = myCalendar.get(Calendar.MINUTE);<br>
timeBean.setSeconds = myCalendar.get(Calendar.SECOND);<br>
request.setAttribute("tempTimeBean", timeBean);<br>
</jsp:scriptlet><br>
======================================================================<br>
<p><b>
View: displayTime2.jsp</b><p><br>
和displayTime.jsp非常相似,但timeByZone2.jsp在也的顶部被调用。请注意 <jsp:useBean />
中的"scope"已经被换成了"request"。<br>
<br>
======================================================================<br>
<xml version="1.0" ?><br>
<H1>Time JSP</H1><br>
<br>
<jsp:include page="timeByZone2.jsp" /><br>
<br>
<jsp:useBean class="TimeBean" id="tempTimeBean" scope="request" /> <br>
<jsp:getProperty name="tempTimeBean" property="hours">:<br>
<jsp:getProperty name="tempTimeBean" property="minutes">:<br>
<jsp:getProperty name="tempTimeBean" property="seconds"><br>
<!-- these would have printed "null" if tempTimeBean was not instantiated by timeByZone2.jsp --><br>
<br>
======================================================================<p><br>
在一个在建系统中,我们已经使用这种方法来创建类的链,每一个都只对它所处理的工作负责。通过辨别公用的表示格式,我们创建了一个View对象,即使在很高层次的JSP中它也可以重复使用。我们的目标就是建立一些可重用的页,同时减少用于表示的类的数量。 <br>
<br>
<b>单个的Servlet Model (A Model II Design)</b><p><br>
什么时候我有足够时间来研究这个课题,我会在这里发表更多的东西。
<br>
<br>
附原文:<p>JSP Architectures<br>
An explanation and comparison of the methodologies<br>
commonly known as "Model I" and "Model II".<br>
Lance Lavandowska To Outline <br>
<br>
If you spend any time reading through Servlet or JSP related newsgroups or
mailing lists, you're likely to encounter a discussion of Model I versus
Model II methodologies . Which one you use depends on personal taste, team
work strategies and OOP orthodoxy. <br>
<br>
Loosely described, Model I is an approach where business logic and
presentation code can be intermixed with the presentation itself (HTML in
our arena). Model II proscribes that all code, to the extent this is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -