《翻译文献-第章使用自定义标签库和jsp标准标签库.doc》由会员分享,可在线阅读,更多相关《翻译文献-第章使用自定义标签库和jsp标准标签库.doc(20页珍藏版)》请在金锄头文库上搜索。
1、大连交通大学2016届本科生毕业设计(论文)外文翻译外文原文Chapter 7. Using Custom Tag Libraries and the JSP Standard Tag LibrarySo far weve covered the JSP basicsthe primary parts of a page and installation and execution of a pageand how to use beans to dynamically add content to a page. Before we start working on real applica
2、tions, lets turn to another fundamental JSP feature: custom tag libraries.Custom tag libraries are, in my opinion, what make JSP so powerful. They make it possible for page authors to embed pretty much any logic in a page using familiar, HTML-like elements. In this chapter, we take a close look at w
3、hat a custom tag library is, how to install and use it, and what the JSP Standard Tag Library (JSTL) brings to the table.7.1 What Is a Custom Tag Library?The JSP standard actions, such as the and actions used in Chapter 6, are HTML-like elements for commonly needed functions in a JSP page: creating
4、beans, accessing bean properties, and invoking other JSP pages. But theres a lot more you want to do that isnt covered by the standard actions.To extend the set of action elements a page author can use in the same familiar way, new actions can be developed, either by a programmer as Java classes or
5、by a page author as tag files (a special kind of JSP file). In either case, these actions are called custom actions. A custom action can do pretty much anything: it has access to all information about the request, it can add content to the response body as well as set response headers, and it can us
6、e any Java API to access external resources such as databases, LDAP servers, or mail servers. The way the JSP container interacts with a custom action also makes it possible for a custom action to conditionally process its body and to abort the processing of the rest of the page. Custom actions can
7、be created for application-specific functions to make it easier for page authors to develop the JSP pages. Some typical examples are shown later in this book.A custom action is inserted into a page using an HTML-like (actually XML) element. The attribute values, and sometimes the body, you provide t
8、ell the action what to do and the data to use. In fact, you have already used a custom action; the action used in Chapter 5 and Chapter 6. Its part of the JSTL core library, and the JSTL libraries are implemented based on the same mechanisms as an application-specific custom tag library.Behind the s
9、cenes, a custom action is implemented either as a Java class or as a tag file. The name of the class or the tag file and other information the container needs to invoke it are specified in a file called a Tag Library Descriptor (TLD). A custom tag library is simply a collection of the TLD and all fi
10、les for a related set of custom actions. In most cases, the TLD and all files are packaged in a JAR file to make it easy to install.Brief Custom Action Introduction for Java ProgrammersI explain in detail how to develop custom actions as tag files in Chapter 11, and as Java classes in Chapter 21, Ch
11、apter 22, and Chapter 23. But if youre a programmer, I know youre curious, so heres a taste of what goes on behind the scene.For a custom action implemented in Java, a class called a tag handler implements the custom action behavior. The tag handler class implements the javax.servlet.jsp.tagext.Simp
12、leTag interface directly or by extending a support class defined by the JSP specification. This is the tag handler for the custom action used in this chapter:package com.ora.jsp.tags.motd; import java.io.*;import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;import com.ora.jsp.beans.motd.*;p
13、ublic class MixedMessageTag extends SimpleTagSupport private MixedMessageBean mmb = new MixedMessageBean( ); / Attributes private String category; public void setCategory(String category) this.category = category; public void doTag( ) throws IOException mmb.setCategory(category); JspWriter out = get
14、JspContext().getOut( ); out.println(mmb.getMessage( ); For each attribute supported by the custom action, the tag handler must implement a bean-style setter method, such as the setCategory( ) method in this example. The container calls methods defined by the SimpleTag interface, such as the doTag( )
15、 method, to let the tag handler do its thing.So why is it called a custom tag library if its a collection of custom actions? Using formal XML terminology, one or more tags (e.g., an opening tag and a closing tag) represent one element (the combination of the tags and possibly a body), but the word tag is commonly used to refer to both tags and elements because its easier to say and shorter to type. Hence, the representation of a custom action (the functionality) in a JSP page is really called a custom action element. But thats just way too many words for most of us to say over and