偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

JSP設(shè)計(jì)模式中的兩種常見(jiàn)模式

開(kāi)發(fā) 后端
本文從架構(gòu)、實(shí)例形象地介紹了JSP設(shè)計(jì)模式的應(yīng)用,包括簡(jiǎn)單的單層次應(yīng)用、重定向請(qǐng)求,更有JSP設(shè)計(jì)模式的源碼實(shí)現(xiàn)。

如果你經(jīng)常去Servlet或JSP的新聞組或者郵件列表,那么一定會(huì)看到不少關(guān)于Model I 和Model II 方法的討論。究竟采用哪一種,這取決于你的個(gè)人喜好、團(tuán)隊(duì)工作策略以及是否采用正統(tǒng)的OOP。

簡(jiǎn)單地說(shuō),Model I將事務(wù)邏輯(business logic)和表示代碼(presentation code)融合在一起(如在HTML中);Model II則提倡最大限度地將所有的代碼放到內(nèi)容表示之外。

Model I: 簡(jiǎn)單的單層次應(yīng)用

如果是在一個(gè)人人都精通Java和HTML的環(huán)境中,或者你獨(dú)自做著所有的工作,假如每個(gè)人都有清晰的編程結(jié)構(gòu)和思路,那么這種方法會(huì)很有效,不過(guò)這樣的假設(shè)不在本文討論范圍之內(nèi)。這種方法的第一個(gè)優(yōu)點(diǎn)是如果你的應(yīng)用改變了,你只需維護(hù)一個(gè)文件。而最大的缺陷是可讀性!除非十分小心,否則你的HTML和Java代碼會(huì)相互混雜,從而難以維護(hù)。

在下面這個(gè)例子中,我們將增加一個(gè) TimeZone 元素,從而使它變成JSP文件,它會(huì)返回基于時(shí)間的所期待的TimeZone。如果沒(méi)有提交 TimeZone,那么缺省的是服務(wù)器的缺省時(shí)間。

  1. ======================================================================   
  2. ﹤xml version=“1.0“ ?﹥   
  3. ﹤H1﹥Time JSP﹤/H1﹥   
  4. ﹤jsp:scriptlet﹥   
  5. //the parameter “zone“ shall be equal to a number between 0 and 24 (inclusive)   
  6. TimeZone timeZone = TimeZone.getDefault(); //returns the default TimeZone for the server   
  7. if (request.getParameterValues(“zone“) != null)   
  8. {   
  9. String timeZoneArg = request.getParameterValues(“zone“)[0];   
  10. timeZone = TimeZone.getTimeZone(“GMT+“ + timeZoneArg + “:00“);   
  11. // gets a TimeZone. For this example we´re just going to assume   
  12. // its a positive argument, not a negative one.   
  13. }   
  14. //since we´re basing our time from GMT, we´ll set our Locale to Brittania, and get a Calendar.   
  15. Calendar myCalendar = Calendar.getInstance(timeZone, Locale.UK);   
  16. ﹤/jsp:scriptlet﹥   
  17. ﹤%= myCalendar.get(Calendar.HOUR_OF_DAY) %﹥:   
  18. ﹤%= myCalendar.get(Calendar.MINUTE) %﹥:   
  19. ﹤%= myCalendar.get(Calendar.SECOND) %﹥   
  20. ======================================================================  

相應(yīng)地,數(shù)據(jù)也可以從JavaBean取得并加以顯示。在下一個(gè)例子中我們就可以看到。

Model II: 重定向請(qǐng)求(Redirecting Requests)

在一個(gè)團(tuán)隊(duì)開(kāi)發(fā)環(huán)境中,有些是HTML設(shè)計(jì)者,另一些則是Java程序員,這時(shí)這一方法顯得非常重要。Java程序員可以集中精力創(chuàng)建可重用代碼,而HTML設(shè)計(jì)師可以集中精力于內(nèi)容表示,彼此相對(duì)對(duì)立,可以分別動(dòng)態(tài)地修改自己的內(nèi)容,只要總體的輸入輸出不變。

現(xiàn)在我們可以使用Model II來(lái)表示Model I的那個(gè)例子。這一方法遵循了Model-View-Controller (MVC) 范例 (cite Design Patterns book)。 在這個(gè)例子中,我們只有一個(gè)類(lèi)(頁(yè)或者servlet) 處理請(qǐng)求(Controller),取得TimeZone,設(shè)置所有用于表示的變量,并將控制傳遞到表示頁(yè)(View)。作為如此簡(jiǎn)單的應(yīng)用,可以沒(méi)有 “Model“。

Controller: timeByZone.jsp

controller可以是一個(gè)servlet或一個(gè)JSP頁(yè)。我推薦使用JSP,因?yàn)檫@樣我不必?fù)?dān)心每當(dāng)我做修改時(shí)要對(duì)類(lèi)重新編譯,但是,你將因此失去granularity(顆粒性),以后要擴(kuò)展該類(lèi)也比較困難。

  1. ======================================================================   
  2. ﹤xml version=“1.0“ ?﹥   
  3. ﹤!--Worker Class, nobody should see me--﹥   
  4. ﹤jsp:scriptlet﹥   
  5. //the parameter “zone“ shall be equal to a number between 0 and 24 (inclusive)   
  6. TimeZone timeZone = TimeZone.getDefault(); //returns the default TimeZone for the server   
  7. if (request.getParameterValues(“zone“) != null)   
  8. {   
  9. String timeZoneArg = request.getParameterValues(“zone“)[0];   
  10. timeZone = TimeZone.getTimeZone(“GMT+“ + timeZoneArg + “:00“);   
  11. // gets a TimeZone. For this example we´re just going to assume   
  12. // its a positive argument, not a negative one.  
  13.  
  14. }   
  15. TimeBean timeBean = new TimeBean();   
  16. timeBean.setHours = myCalendar.get(Calendar.HOUR_OF_DAY);   
  17. timeBean.setMinutes = myCalendar.get(Calendar.MINUTE);   
  18. timeBean.setSeconds = myCalendar.get(Calendar.SECOND);   
  19. HttpSession mySession = request.getSession();   
  20. mySession.putValue(“tempTimeBean“, timeBean);   
  21. ﹤/jsp:scriptlet﹥   
  22. ﹤jsp:forward page=“displayTime.jsp“ /﹥   
  23. ======================================================================  

View: displayTime.jsp

同樣地,這個(gè)view既可以是一個(gè)servlet也可以是一個(gè)jsp文件。這里我們從Session中取得并顯示它的值。實(shí)際上我們會(huì)將這做兩次,來(lái)示范Bean是如何被使用的。

  1. ======================================================================   
  2. ﹤xml version=“1.0“ ?﹥   
  3. ﹤H1﹥Time JSP﹤/H1﹥   
  4. ﹤jsp:useBean class=“TimeBean“ id=“tempTimeBean“ scope=“session“ /﹥   
  5. ﹤jsp:getProperty name=“tempTimeBean“ property=“hours“﹥:   
  6. ﹤jsp:getProperty name=“tempTimeBean“ property=“minutes“﹥:   
  7. ﹤jsp:getProperty name=“tempTimeBean“ property=“seconds“﹥   
  8. ﹤!-- these would have printed “null“ if tempTimeBean was not instantiated by timeByZone.jsp --﹥   
  9. ﹤jsp:scriptlet﹥   
  10. HttpSession mySession = request.getSession();   
  11. TimeBean timeBean = mySession.getValue(“tempTimeBean“);   
  12. if (timeBean != null)   
  13. { // check to make sure its not null, to avoid NullPointerExceptions   
  14. out.print(timeBean.getHours());   
  15. out.print(“:“);   
  16. out.print(timeBean.getMinutes());   
  17. out.print(“:“);   
  18. out.print(timeBean.getSeconds());   
  19. }   
  20. else   
  21. {   
  22. out.println(“Press your Back button and select a TimeZone“);   
  23. }   
  24. ﹤/jsp:scriptlet﹥   
  25. ======================================================================  

#p# 

第二種方法(在內(nèi)部使用了代碼)可能有些笨重,但允許開(kāi)發(fā)者確保輸出不至于很糟糕(例如“null:null:null null“),假定Session bean還沒(méi)有被實(shí)例化以及沒(méi)有進(jìn)行值的設(shè)置。 這種情況發(fā)生在客戶(hù)端直接調(diào)用了View頁(yè)。問(wèn)題是使用腳本scriptlets可以允許更強(qiáng)的控制。如果你確信你可以控制url存取,那么bean方法當(dāng)然更適合于開(kāi)發(fā),并使 View頁(yè)更方便于HTML設(shè)計(jì)者的協(xié)同工作。

上面的是“傳統(tǒng)的“ Model II設(shè)計(jì)。所有的變量都包裝了并放在Session對(duì)象中。這有2個(gè)不足:

1) 如果客戶(hù)端拒絕參與的話(huà),Session是不可得到的。
 
2) 除非Session變量被顯式地移走,否則它回一直存在,直到Session被破壞或過(guò)期。

第一種案例很可能發(fā)生在這樣的場(chǎng)合,即使用了cookies作為聲明的結(jié)構(gòu)(mechanism)而開(kāi)發(fā)者沒(méi)有能夠提供聲明的結(jié)構(gòu)的替代表單(form),即URL改寫(xiě)。

第二個(gè)案例甚至更為嚴(yán)重,因?yàn)樗赡芤鸷艽蟮膬?nèi)存消耗,如果Sessions被定義為保存比標(biāo)準(zhǔn)存留時(shí)間更長(zhǎng)的話(huà)((標(biāo)準(zhǔn)存留時(shí)間是30分鐘)。即使是30分鐘的Session,這種Model也可能在大的應(yīng)用中引起災(zāi)難性的內(nèi)存泄露。為什么呢?在Session對(duì)象內(nèi)部設(shè)置的對(duì)象被實(shí)例化了,并且在Session終止以前一直沒(méi)有被移去。因?yàn)樗鼈內(nèi)匀挥嘘P(guān)聯(lián)references(Session對(duì)象) 指向它們,所以無(wú)法被垃圾收集(garbage-collected)。

在Model II 模型中,很多對(duì)象被放到Session中(要么直接地,要么通過(guò)JavaBean)。隨著Session的進(jìn)行,更多的頁(yè)被存取,內(nèi)存使用會(huì)增加并持續(xù)下去直到客戶(hù)端終止了Session或者Session過(guò)期。要一直等到Session變得非法,放在那的對(duì)象才能被垃圾收集,而那些損失的內(nèi)存本可以用于任何其它的用途。.

改進(jìn)的方法之一是將Beans或者其它變量放到Request對(duì)象中去,并使用RequestDispatcher.include()而不是RequestDispatcher.forward()。這樣做以后,View 頁(yè)具有和Controller一樣的存取請(qǐng)求的對(duì)象。傳統(tǒng)的Model II設(shè)計(jì)的不足可以被排除。

一個(gè)最后的評(píng)注:盡管有如上所述,我個(gè)人仍有些不喜歡Model II 的范例,如果它用通常方法開(kāi)發(fā)的話(huà)。 客戶(hù)端被引送到某一個(gè)地址,然后又被轉(zhuǎn)向到另一個(gè)不同的類(lèi),我不喜歡創(chuàng)建這樣的系統(tǒng)。基于這樣的原因,我修改了設(shè)計(jì),使它變成了以下的樣子:

Controller: timeByZone2.jsp

和前面一樣,controller使用Request值來(lái)取得必要的數(shù)據(jù),并且將數(shù)據(jù)放到請(qǐng)求的對(duì)象中去。這回的區(qū)別是View頁(yè)將使用RequestDispatcher.include()來(lái)調(diào)用Controller。在這種方法中,客戶(hù)端再也不做重定向,請(qǐng)求不是“鏈接chained”的。相當(dāng)于class/jsp請(qǐng)求了另一方來(lái)為它做一些工作,然后繼續(xù)。

  1. ======================================================================   
  2. ﹤xml version=“1.0“ ?﹥   
  3. ﹤!--Worker Class, nobody should see me--﹥   
  4. ﹤jsp:scriptlet﹥   
  5. //the parameter “zone“ shall be equal to a number between 0 and 24 (inclusive)   
  6. TimeZone timeZone = TimeZone.getDefault(); //returns the default TimeZone for the server   
  7. if (request.getParameterValues(“zone“) != null)   
  8. {   
  9. String timeZoneArg = request.getParameterValues(“zone“)[0];   
  10. timeZone = TimeZone.getTimeZone(“GMT+“ + timeZoneArg + “:00“);   
  11. // gets a TimeZone. For this example we´re just going to assume   
  12. // its a positive argument, not a negative one.   
  13. }   
  14. TimeBean timeBean = new TimeBean();   
  15. timeBean.setHours = myCalendar.get(Calendar.HOUR_OF_DAY);   
  16. timeBean.setMinutes = myCalendar.get(Calendar.MINUTE);   
  17. timeBean.setSeconds = myCalendar.get(Calendar.SECOND);   
  18. request.setAttribute(“tempTimeBean“, timeBean);   
  19. ﹤/jsp:scriptlet﹥   
  20. ======================================================================  

View: displayTime2.jsp

和displayTime.jsp非常相似,但timeByZone2.jsp在也的頂部被調(diào)用。請(qǐng)注意 中的“scope“已經(jīng)被換成了“request“。

  1. ======================================================================   
  2. ﹤xml version=“1.0“ ?﹥   
  3. ﹤H1﹥Time JSP﹤/H1﹥   
  4. ﹤jsp:include page=“timeByZone2.jsp“ /﹥   
  5. ﹤jsp:useBean class=“TimeBean“ id=“tempTimeBean“ scope=“request“ /﹥   
  6. ﹤jsp:getProperty name=“tempTimeBean“ property=“hours“﹥:   
  7. ﹤jsp:getProperty name=“tempTimeBean“ property=“minutes“﹥:   
  8. ﹤jsp:getProperty name=“tempTimeBean“ property=“seconds“﹥   
  9. ﹤!-- these would have printed “null“ if tempTimeBean was not instantiated by timeByZone2.jsp --﹥   
  10. ======================================================================   
在一個(gè)在建系統(tǒng)中,我們已經(jīng)使用這種方法來(lái)創(chuàng)建類(lèi)的鏈,每一個(gè)都只對(duì)它所處理的工作負(fù)責(zé)。通過(guò)辨別公用的表示格式,我們創(chuàng)建了一個(gè)View對(duì)象,即使在很高層次的JSP中它也可以重復(fù)使用。我們的目標(biāo)就是建立一些可重用的頁(yè),同時(shí)減少用于表示的類(lèi)的數(shù)量

【編輯推薦】

  1. 對(duì)JSP中的內(nèi)置對(duì)象簡(jiǎn)單概述
  2. JSP和Servlet中的幾個(gè)編碼的作用及原理
  3. JSP標(biāo)簽庫(kù)解析
  4. JSP設(shè)計(jì)模式淺析
  5. 什么是JSP以及其強(qiáng)弱勢(shì)
責(zé)任編輯:仲衡 來(lái)源: 神州學(xué)習(xí)網(wǎng)
相關(guān)推薦

2009-07-06 17:46:41

JSP網(wǎng)站

2024-10-14 08:39:29

工廠模式策略模式代碼

2024-06-06 08:32:52

.NET框架代碼

2020-10-09 06:52:31

設(shè)計(jì)模式軟件

2010-07-02 10:28:41

虛擬化桌面虛擬化

2011-02-23 12:49:31

KonquerorEmbedded

2022-02-21 08:18:38

option編程模式

2010-08-26 15:15:18

DB2備份

2010-06-02 15:29:06

SVN版本控制

2012-11-29 10:45:31

2021-10-09 09:15:01

Windows 11安全模式系統(tǒng)

2021-04-20 22:09:13

Python編程語(yǔ)言

2009-06-29 17:39:31

JSP設(shè)計(jì)模式

2020-09-11 10:36:24

設(shè)計(jì)模式代碼

2011-06-22 14:14:27

pageEncodincontentType

2009-12-17 13:45:58

VS 2008外殼

2010-08-31 09:31:58

Silverlight

2022-03-10 07:39:33

.NET部署模式

2022-02-02 21:29:39

路由模式Vue-Router

2021-03-07 22:37:17

Go代碼模式
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)