?? 0192.htm
字號:
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設計者的協同工作。<br>
<br>
<br>
上面的是"傳統的" Model II設計。所有的變量都包裝了并放在Session對象中。這有2個不足:<br>
<br>
1) 如果客戶端拒絕參與的話,Session是不可得到的。<br>
<br>
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>
Controller: timeByZone2.jsp<br>
<br>
<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>
<br>
<br>
View: displayTime2.jsp<br>
<br>
<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>
======================================================================<br>
<br>
<br>
在一個在建系統中,我們已經使用這種方法來創建類的鏈,每一個都只對它所處理的工作負責。通過辨別公用的表示格式,我們創建了一個View對象,即使在很高層次的JSP中它也可以重復使用。我們的目標就是建立一些可重用的頁,同時減少用于表示的類的數量。 <br>
<br>
單個的Servlet Model (A Model II Design)<br>
<br>
<br>
什么時候我有足夠時間來研究這個課題,我會在這里發表更多的東西。 <br>
<br>
附原文:<br>
<br>
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 possible, be excluded from the presentation. <br>
<br>
Model I: Simple 2 1/2 Tier Application<br>
In a team environment where everyone knows Java and HTML, or if you're doing it all yourself, this approach can work well, provided everyone maintains a clear coding structure (that discussion is outside the bounds of this article). The primary advantage of this approach is that there is only one file to maintain for changes to your application. The major disadvantage is readability! Unless great care is taken, your HTML and Java code can become so intermingled that it becomes difficult to debug and maintain your application. <br>
<br>
For this example, we are going to revisit the "Sample Page" from the JSP Quick Start chapter. I'm going to add a TimeZone element, so we'll have a JSP that returns the time based on the desired timezone. If no TimeZone is submitted, we'll default to that of the server.<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>
Similarly, the data to be displayed could have been gotten from a JavaBean. We'll see a little of that in the next example. <br>
Model II: Redirecting Requests<br>
In a team environment where some members are HTML designers and others are Java programmers, this approach can be particularly strong. The Java programmers can focus on creating (re)usable code, while the HTML designers can focus on presentation. While the two remain dependant on each other, one or the other can change dramatically so long as the principle inputs and outputs (respectively) remain the same. <br>
<br>
Now we'll take the same desired behaviour from the Model I example, and present it using the Model II methodology. This methodology follows the Model-View-Controller (MVC) paradigm (cite Design Patterns book). For this example, we'll have one class (or page or servlet) process the request (Controller), get the TimeZone, set all the required variables for presentation, and pass control off to a presentation page (View). For simple apps like this, there is no "Model". <br>
<br>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -