Jsp最佳实践外文翻译@中英文翻译@外文文献翻译_第1页
Jsp最佳实践外文翻译@中英文翻译@外文文献翻译_第2页
Jsp最佳实践外文翻译@中英文翻译@外文文献翻译_第3页
Jsp最佳实践外文翻译@中英文翻译@外文文献翻译_第4页
Jsp最佳实践外文翻译@中英文翻译@外文文献翻译_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

沈阳航空工业学院毕业设计(论文) 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 沈阳航空工业学院毕业设计(论文) 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 沈阳航空工业学院毕业设计(论文) 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 沈阳航空工业学院毕业设计(论文) 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 沈阳航空工业学院毕业设计(论文) 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 沈阳航空工业学院毕业设计(论文) 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. 沈阳航空工业学院毕业设计(论文) 1 Jsp最佳实践 Jsp 技术是 servlet 技术的扩展,结合 html、 java 代码于一个文件。 Java servlet技术关注于利用 PrintWriter.println()语句产生 html 输出的 java 类, Jsp 将这个概念抽象到一个更高的层次。使用 jsp, web 开发者可以写静态的 html 和将 java 代码片段加入到需要动态产生的页面中,从而,这 种灵活的技术使简单 web 应用的快速开发成为可能。然而它能被滥用,从而形成难以维护、重用和改进的不必要的复杂的应用软件。 遵循以下提示的技巧可以避免这种不必要的复杂应用。 1、 分离 html和 java 2、 将业务逻辑放在 javaBean 中 3、 从标签定制管理器类中分离出常用行为 4、 较之 java 代码在 jsps 中,更倾向于 html 在 java 管理器类中 5、 使用适当的包含机制 6、 使用 jsp 模版机制 7、 使用 CSS 样式表 8、 使用 MVC 模式 9、 使用有效的标签定制库。 10、 尽可能多使用 jsp 注 释 11、 遵循 html最佳实践 12、 利用 jsp 异常机制 这些可帮助你写出可重用、易维护的 jsp 一、分离 html和 java 将一个 web 页的所有必须的 java、 html 代码放入一个 jsp 文件中是诱人的。这种方法使初学者定位相关联的代码和理解它们如何相互作用变的容易。然而,当应用变的更加复杂、开发者变的更加棘手时,这样方式将变的更加繁重和昂贵。 沈阳航空工业学院毕业设计(论文) 2 结合 html 和 java 在同一的代码来源使程序变的非常不可读。为增强可读性,很多开发者使用缩排格式,但是混合 html 和 java 片段的文件使有益的缩排格式变的极其难 以维护。 许多 web 开发方法和机制强调 html 和 java 代码的分离,从而不同的开发者可以将精力集中在他们擅长的方面。适当地将 java 代码和包括 jsp 标签、定制标签在内的 html 分离,可以使 web 设计者、 html 编写者工作在 html(表述)方面,而 java开发者工作在应用的逻辑处理部分。当 Java 开发者实现 jsp 定制标签后的行为时关注的是业务逻辑, web 设计者则象使用普通 html 标签一样使用这些定制标签。 二、将业务逻辑放在 JavaBean中 直接包含在 jsp中的 java代码并不象包含在 JavaBean中的 java代码那样容易被其他 jsp 页面理解,通用行为和业务逻辑放在 JavaBean 中不仅可以被其它 jsp,也可以被应用的其它部分使用,这是因为 JavaBean仅仅是满足一些基本约定(比如不含参数的构造器,为 private 类属性设置 set/get 方法)的 java 类,也能作为任意其它类使用。值得注意的是, ejb 在封装针对应用中所有组件通用的行为和数据时也是有用的。 三、从标签定制管理器类中分离出常用行为 作为定制标签管理器类的 java 类实现定制标签,并不象 JavaBean,它不能如普通 java 工具类一样易于使用,而 是,定制标签管理器类实现特定的接口或继承提供这些接口基本实现的类。由于它们不易于在 jsp 外使用,定制标签管理器类应当仅包含那些不能在定制标签之外、 jsp 之外使用的特定行为。定制标签常常需要针对通用行为和业务逻辑的支撑,并利用提供通用行为的 JavaBeans 和 EJBs 四、较之 java 代码在 jsp 中,更倾向于 html 在 java 管理器类中 有时从 java 中分离 html、 jsp 标签和如定制标签的 html 会需要不必要的令人费解的代码,基于此,你要么将 java 片段和表述放入 jsp 中,要么将 html 代码放入 java标签 管理器类。 较之看到在 jsp 中作为脚本的 java,我更愿意看到在 java 类中的一小部分 html代码。由于定制标签管理器针对它们所实现的定制标签是特定的(同时也不能在 jsp之外使用),放入一些 html 代码不会有什么麻烦, SUN 的 J2EE蓝皮书对此有更深入的讨论。 沈阳航空工业学院毕业设计(论文) 3 对此标准也有例外:如果在 jsp 中包含一行或两行 java 代码片段和在 java 管理器类中包含许多行 html 代码解决的问题一样,那么允许在 jsp 中存在 java 代码应该是明智的。 五、使用适当的包含机制 包含机制在代码重用方面是少有的好的设计。从多个页面中分 离出通用的 jsp和 html代码放入一个文件可以提高可维护性(仅需要在一处改变)和可重用性。 有两种包含机制缩小了代码冗余促进了代码重用。为确保能够使用适当的包含机制,理解它们二者间的不同是重要的。除非我可以证明需要 include 动作是正当的,一般地情况下我使用 include 指令。在蓝皮书“ web 层”部分中的第七个问题,对理解两种包含机制的不同和确定在一特定情况使用哪一种提供了很好的资源。 六、使用 jsp 模版机制 一个模版机制允许一个公用的文件来控制 web 页、 jsp、页面布局。于是,当你想改变页面布局时, 你仅仅需要修改一个文件,所有其它的页面将反映出页面布局的改变。这不仅是使代码更加具有可维护性,页面布局模版机制对那些看到所有应用软件页面都协调一致的用户来说,使 web 页面显得更加美观和友好。 七、使用 CSS 样式表 正如模版可以使开发者将页面布局控制放于一处,样式表可以使开发者将外观控制放于一处。我使用 CSS 样式表来控制诸如字体格式、尺寸,表特征等项目。象模版一样,样式表允许开发者在一处改变,这些改变会立刻映射到所有外观页面,从而促进可维护性和给用户一致的外观。 八、使用 MVC 设计模式 当然、其它设计模式可以 在 jsp 中有效的使用,而我经常使用模型 ?视图 ?

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论