已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
沈陽航空工業(yè)學(xué)院畢業(yè)設(shè)計(論文) 1 JSP best practices JavaServer Pages (JSPs) technology is an extension of Java servlet technology and combines HTML and Java code into a single file. While Java servlet technology focuses on Java classes capable of generating HTML output with PrintWriter.println() statements, JSP technology abstracts this concept to a higher level. With JavaServer Pages, a Web developer can write static HTML pages and simply add Java code in those sections of the page that need to be dynamically generated. While this flexibility enables rapid development of simple Web applications, it can be abused, resulting in unnecessarily complex applications that are difficult to maintain, reuse, and enhance. To avoid needlessly complex applications, follow the practices I present in this article: 1. Separate HTML from Java 2. Place business logic in JavaBeans 3. Factor general behavior out of custom tag handler classes 4. Favor HTML in Java handler classes over Java in JSPs 5. Use an appropriate inclusion mechanism 6. Use a JSP template mechanism 7. Use stylesheets 8. Use the MVC pattern 9. Use available custom tag libraries 10. Use JSP comments in most cases 11. Follow HTML best practices 12. Utilize the JSP exception mechanism These tips will help you write JSPs that are reusable and easy to maintain. (1)Separate HTML from Java It can be tempting to throw all Java and HTML code necessary for a Webpage into a single JSP file. In simple system development, such an approach makes it easy for someone new to the system to locate all relevant code in one place and understand how it 沈陽航空工業(yè)學(xué)院畢業(yè)設(shè)計(論文) 2 all interacts. However, this approach becomes burdensome and costly when the application grows more complex and more developers become involved. Combining HTML and Java in the same source code can make the code significantly less readable. To enhance software readability, developers often use indentation; but mixing HTML and Java scriptlets in the same file can make useful indentation extremely difficult to maintain. Many Web development methodologies and architectures now emphasize the separation of HTML from Java code so different developers can focus on their strengths. Properly separating Java and HTML, including HTML-like JSP tags and custom tags, allows Web designers and HTML coders to work on the HTML (presentation) aspects, while Java developers work on the application s Java (processing logic) portions. Java developers focus on business logic as they implement the behavior behind the custom tags; Web designers then use these custom tags just as they use ordinary HTML tags. (2)Place business logic in JavaBeans Java code included directly inside a JSP is not as readily accessible to other JSPs as Java code contained within a JavaBean. Common behavior and business logic placed in JavaBeans can not only be used by other JSPs but also by other portions of the application. That is because JavaBeans are merely Java classes that satisfy some basic conventions (such as a constructor with no arguments and public get/set methods for private data members) and can be used as any other Java class. Note that Enterprise JavaBeans (EJBs) are also useful for storing behaviors and data common to all components of the application. (3)Factor general behavior out of custom tag handler classes Java classes known as custom tag handlers implement custom tags. Unlike JavaBeans, custom tag handler classes are not readily used like ordinary Java utility classes. Instead, custom tag handler classes implement specific interfaces - or extend classes that provide these interfaces basic implementations. Because they are not readily reused outside JSPs, custom tag handlers should contain only specific behavior that would not be useful outside that custom tag - that is, outside the JSP. Custom tags often require support for common behaviors or business logic and can utilize JavaBeans or EJBs that perform those common behaviors. (4)Favor HTML in Java handler classes over Java in JSPs 沈陽航空工業(yè)學(xué)院畢業(yè)設(shè)計(論文) 3 Sometimes cleanly separating HTML, JSP tags, and HTML-like custom tags from Java requires unnecessarily convoluted code. In these cases, you either include Java scriptlets and expressions in the JSP or put some HTML code in the Java tag handler class. I d rather see a small amount of HTML code in the Java class than see Java, such as scriptlets and expressions, in the JSP. Since custom tag handlers are specific to the custom tags they implement (and not reusable outside JSPs), placing necessary HTML there is not troublesome. Sun s Java 2 Platform, Enterprise Edition (J2EE) Blueprints documentation discusses this issue further. (5)Use an appropriate inclusion mechanism It is rarely good design to reproduce code commonly used by different application pieces each time another piece of that application needs that functionality. Factoring common JSP or HTML code out of multiple pages and into a single file improves maintainability (you need to make changes in only one location) and reusability. Two JSP include mechanisms reduce code redundancy and promote reusability; to ensure that you use the appropriate include mechanism, it is important to know the differences between the two. Generally, I use the include directive unless I can justify a need for the include action. Question 7 in the Blueprints Web Tier section provides a good resource for understanding the differences between the two include mechanisms and determining which to use in a particular situation. (6)Use a JSP template mechanism A template mechanism allows for a common file to control Webpage, or JSP, layout. Then, when you want to change the layout, you need to modify only one file, and all the other pages will reflect the layout change. This doesn t just make for more maintainable code; using templates to control layout also makes Webpages more aesthetically pleasing to users who see consistent layouts for all an application s pages. (7)Use stylesheets Just as templates enable developers to place layout control in a single location, stylesheets enable developers to place appearance control in a single location. I use Cascading Style Sheets (CSS) to control such items as font families, font sizes, and table characteristics. Like templates, stylesheets allow the developer to make changes in one 沈陽航空工業(yè)學(xué)院畢業(yè)設(shè)計(論文) 4 location; those changes immediately reflect on all appropriate pages, resulting in increased maintainability and consistent appearance to users. (8)Use the MVC pattern While other design patterns can be used effectively with JSPs, I often use the Model-View-Controller (MVC) architecture with JSP technology. MVC enables the development of applications that are easier to create, test, maintain, and enhance. In JSP terminology, implementation of an MVC architecture is often referred to as Model 2 (from an early JSP specification). The J2EE Blueprints samples are based on MVC. (9)Use available custom tag libraries Why should developers spend time reinventing the wheel and worrying about testing and debugging when custom tag libraries are readily available for many different purposes? Some vendors provide custom tag libraries to their customers for free or for individual purchase, but many custom tags can be found online. Resources provides a good starting point for locating potentially useful tag libraries. While these third-party custom tag libraries occasionally have bugs, most likely such problems will be discovered, since many developers use these libraries and test them in their own applications. Also, many custom tags are open source, so you can edit them to meet your needs. I find it well worth my time to keep informed of available custom tags, since these libraries often provide functionality common to most Web applications. While learning about available custom tag libraries requires a small time investment, reusing already-available custom tags saves the time of writing, testing, and debugging my own custom tags. As mentioned above, many tag libraries are also open source; in these cases, I can readily adapt general behavior to my specific project s situation. (10)Use JSP comments in most cases Appropriate commenting seems to challenge software developers. JSPs, like other types of code, should include comments that describe complex or extraordinary functionality, the pages purpose, and other general information typically commented out in source code. Since JSPs allow developers to intermix Java, JSP tags, and HTML tags in the same page, there are multiple ways to comment a JSP page. Developers should carefully consider which type of comment to employ in the page. HTML comments will be 沈陽航空工業(yè)學(xué)院畢業(yè)設(shè)計(論文) 5 viewable in the compiled JSP s HTML source code, and both major browsers make viewing this source easy. JSP comments, on the other hand, are not placed in the HTML document created by the JSP compilation process. These comments cannot be viewed as part of the page s source through the browser, and they do not increase the size of the rendered page s generated source. Java comments can also occur in a JSP inside Java scriptlet sections. These are not viewable in the browser either, but including Java comments in the JSP page violates the principle of separating Java from the HTML. Code comments are usually meant for developers who write and maintain code. Therefore, use JSP comments unless there is a compelling reason to have the comments display in the browser upon request. (11)Follow HTML best practices When Java is factored out of the JSP and into JavaBeans and custom tag handlers, the JSP consists mostly of JSP tags, including custom tags, and HTML tags. To make the JSP easier to understand and maintain, follow best practices related to HTML development. (12)Utilize the JSP exception mechanism While a thrown exception s stack trace proves extremely useful for developers when debugging their code, it is rarely desirable to share an entire exception stack trace with the software s users. Lengthy stack traces are not aesthetically pleasing and can increase security risks by exposing information that does not need to be released. JSPs allow developers to catch and handle exceptions in the code, resulting in more secure and aesthetically pleasing exception handling. See Resources for details on the mechanics of JSP exception handling. Exception information is more useful if information besides the stack trace is included. JSPs can use session variables to store information about the current page and current operation being performed. Then, if an exception does occur, the exception page will be called; it will have access to both the thrown exception and the information about the original page that caused the exception. The exception page can utilize underlying Java code, in JavaBeans or EJBs, to store in the database the complete exception information, related session information, and the exception s date and time. To reduce the unsightly error messages printed to the screen and improve security, the exception page need only print out a simple error message and perhaps an identifying 沈陽航空工業(yè)學(xué)院畢業(yè)設(shè)計(論文) 6 number that allows developers to locate more detailed exception information in the database. For aesthetic and security reasons, I prefer storing most of the exception information in a database or flat file rather than printing it all to the screen. Storing the exception information in a database or flat file also allows the information to be persisted even when a user exits the application. Note that during development you should print full exception information to the screen for regular testing and debugging. 沈陽航空工業(yè)學(xué)院畢業(yè)設(shè)計(論文) 1 Jsp最佳實(shí)踐 Jsp 技術(shù)是 servlet 技術(shù)的擴(kuò)展,結(jié)合 html、 java 代碼于一個文件。 Java servlet技術(shù)關(guān)注于利用 PrintWriter.println()語句產(chǎn)生 html 輸出的 java 類, Jsp 將這個概念抽象到一個更高的層次。使用 jsp, web 開發(fā)者可以寫靜態(tài)的 html 和將 java 代碼片段加入到需要動態(tài)產(chǎn)生的頁面中,從而,這 種靈活的技術(shù)使簡單 web 應(yīng)用的快速開發(fā)成為可能。然而它能被濫用,從而形成難以維護(hù)、重用和改進(jìn)的不必要的復(fù)雜的應(yīng)用軟件。 遵循以下提示的技巧可以避免這種不必要的復(fù)雜應(yīng)用。 1、 分離 html和 java 2、 將業(yè)務(wù)邏輯放在 javaBean 中 3、 從標(biāo)簽定制管理器類中分離出常用行為 4、 較之 java 代碼在 jsps 中,更傾向于 html 在 java 管理器類中 5、 使用適當(dāng)?shù)陌瑱C(jī)制 6、 使用 jsp 模版機(jī)制 7、 使用 CSS 樣式表 8、 使用 MVC 模式 9、 使用有效的標(biāo)簽定制庫。 10、 盡可能多使用 jsp 注 釋 11、 遵循 html最佳實(shí)踐 12、 利用 jsp 異常機(jī)制 這些可幫助你寫出可重用、易維護(hù)的 jsp 一、分離 html和 java 將一個 web 頁的所有必須的 java、 html 代碼放入一個 jsp 文件中是誘人的。這種方法使初學(xué)者定位相關(guān)聯(lián)的代碼和理解它們?nèi)绾蜗嗷プ饔米兊娜菀?。然而,?dāng)應(yīng)用變的更加復(fù)雜、開發(fā)者變的更加棘手時,這樣方式將變的更加繁重和昂貴。 沈陽航空工業(yè)學(xué)院畢業(yè)設(shè)計(論文) 2 結(jié)合 html 和 java 在同一的代碼來源使程序變的非常不可讀。為增強(qiáng)可讀性,很多開發(fā)者使用縮排格式,但是混合 html 和 java 片段的文件使有益的縮排格式變的極其難 以維護(hù)。 許多 web 開發(fā)方法和機(jī)制強(qiáng)調(diào) html 和 java 代碼的分離,從而不同的開發(fā)者可以將精力集中在他們擅長的方面。適當(dāng)?shù)貙?java 代碼和包括 jsp 標(biāo)簽、定制標(biāo)簽在內(nèi)的 html 分離,可以使 web 設(shè)計者、 html 編寫者工作在 html(表述)方面,而 java開發(fā)者工作在應(yīng)用的邏輯處理部分。當(dāng) Java 開發(fā)者實(shí)現(xiàn) jsp 定制標(biāo)簽后的行為時關(guān)注的是業(yè)務(wù)邏輯, web 設(shè)計者則象使用普通 html 標(biāo)簽一樣使用這些定制標(biāo)簽。 二、將業(yè)務(wù)邏輯放在 JavaBean中 直接包含在 jsp中的 java代碼并不象包含在 JavaBean中的 java代碼那樣容易被其他 jsp 頁面理解,通用行為和業(yè)務(wù)邏輯放在 JavaBean 中不僅可以被其它 jsp,也可以被應(yīng)用的其它部分使用,這是因?yàn)?JavaBean僅僅是滿足一些基本約定(比如不含參數(shù)的構(gòu)造器,為 private 類屬性設(shè)置 set/get 方法)的 java 類,也能作為任意其它類使用。值得注意的是, ejb 在封裝針對應(yīng)用中所有組件通用的行為和數(shù)據(jù)時也是有用的。 三、從標(biāo)簽定制管理器類中分離出常用行為 作為定制標(biāo)簽管理器類的 java 類實(shí)現(xiàn)定制標(biāo)簽,并不象 JavaBean,它不能如普通 java 工具類一樣易于使用,而 是,定制標(biāo)簽管理器類實(shí)現(xiàn)特定的接口或繼承提供這些接口基本實(shí)現(xiàn)的類。由于它們不易于在 jsp 外使用,定制標(biāo)簽管理器類應(yīng)當(dāng)僅包含那些不能在定制標(biāo)簽之外、 jsp 之外使用的特定行為。定制標(biāo)簽常常需要針對通用行為和業(yè)務(wù)邏輯的支撐,并利用提供通用行為的 JavaBeans 和 EJBs 四、較之 java 代碼在 jsp 中,更傾向于 html 在 java 管理器類中 有時從 java 中分離 html、 jsp 標(biāo)簽和如定制標(biāo)簽的 html 會需要不必要的令人費(fèi)解的代碼,基于此,你要么將 java 片段和表述放入 jsp 中,要么將 html 代碼放入 java標(biāo)簽 管理器類。 較之看到在 jsp 中作為腳本的 java,我更愿意看到在 java 類中的一小部分 html代碼。由于定制標(biāo)簽管理器針對它們所實(shí)現(xiàn)的定制標(biāo)簽是特定的(同時也不能在 jsp之外使用),放入一些 html 代碼不會有什么麻煩, SUN 的 J2EE藍(lán)皮書對此有更深入的討論。 沈陽航空工業(yè)學(xué)院畢業(yè)設(shè)計(論文) 3 對此標(biāo)準(zhǔn)也有例外:如果在 jsp 中包含一行或兩行 java 代碼片段和在 java 管理器類中包含許多行 html 代碼解決的問題一樣,那么允許在 jsp 中存在 java 代碼應(yīng)該是明智的。 五、使用適當(dāng)?shù)陌瑱C(jī)制 包含機(jī)制在代碼重用方面是少有的好的設(shè)計。從多個頁面中分 離出通用的 jsp和 html代碼放入一個文件可以提高可維護(hù)性(僅需要在一處改變)和可重用性。 有兩種包含機(jī)制縮小了代碼冗余促進(jìn)了代碼重用。為確保能夠使用適當(dāng)?shù)陌瑱C(jī)制,理解它們二者間的不同是重要的。除非我可以證明需要 include 動作是正當(dāng)?shù)?,一般地情況下我使用 include 指令。在藍(lán)皮書“ web 層”部分中的第七個問題,對理解兩種包含機(jī)制的不同和確定在一特定情況使用哪一種提供了很好的資源。 六、使用 jsp 模版機(jī)制 一個模版機(jī)制允許一個公用的文件來控制 web 頁、 jsp、頁面布局。于是,當(dāng)你想改變頁面布局時, 你僅僅需要修改一個文件,所有其它的頁面將反映出頁面布局的改變。這不僅是使代碼更加具有可維護(hù)性,頁面布局模版機(jī)制對那些看到所有應(yīng)用軟件頁面都協(xié)調(diào)一致的用戶來說,使 web 頁面顯得更加美觀和友好。 七、使用 CSS 樣式表 正如模版可以使開發(fā)者將頁面布局控制放于一處,樣式表可以使開發(fā)者將外觀控制放于一處。我使用 CSS 樣式表來控制諸如字體格式、尺寸,表特征等項(xiàng)目。象模版一樣,樣式表允許開發(fā)者在一處改變,這些改變會立刻映射到所有外觀頁面,從而促進(jìn)可維護(hù)性和給用戶一致的外觀。 八、使用 MVC 設(shè)計模式 當(dāng)然、其它設(shè)計模式可以 在 jsp 中有效的使用,而我經(jīng)常使用模型 ?視圖 ?
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030全球開放式框架工業(yè)顯示器行業(yè)調(diào)研及趨勢分析報告
- 2025年全球及中國平盤電滑環(huán)行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 2025-2030全球TGV基板行業(yè)調(diào)研及趨勢分析報告
- 2025年全球及中國完全生物基聚酰胺行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 幼兒繪本講述與演繹幼兒繪本講述的停連運(yùn)用技巧講解
- 2025景區(qū)商場蛇年新春嘉年華活動策劃方案
- 2025綠洲集團(tuán)工程合同管理規(guī)范
- 沙石采購合同范本工程合同
- 2025【合同范本】打印機(jī)耗材長期供貨合同
- 防雷技術(shù)服務(wù)合同
- 第2課+古代希臘羅馬(教學(xué)設(shè)計)-【中職專用】《世界歷史》(高教版2023基礎(chǔ)模塊)
- 中儲糧蘭州公司考試筆試題庫
- 焊接機(jī)器人在汽車制造中應(yīng)用案例分析報告
- 重建成長型思維課件
- 電捕焦油器火災(zāi)爆炸事故分析
- 質(zhì)量問題分析及措施報告
- 汽修廠安全風(fēng)險分級管控清單
- 現(xiàn)代通信原理與技術(shù)(第五版)PPT全套完整教學(xué)課件
- 病例展示(皮膚科)
- DB31T 685-2019 養(yǎng)老機(jī)構(gòu)設(shè)施與服務(wù)要求
- 燕子山風(fēng)電場項(xiàng)目安全預(yù)評價報告
評論
0/150
提交評論