(Transfer) Master the JSP custom label: (2)

zhaozj2021-02-16  91

Simple label example

Page 1 (9 pages a total)

Struts framework has several custom label libraries (see Resources for more information on Struts). One tab in these libraries can create a link to rewrite the URL and encode the rewritten connection with jsessionID. However, there is a problem: if you want to pass a set of request parameters (such as query strings), you may have to create a Java scripTlet for this. It's really chaotic! The following list (Search_Results.jap) shows a JSP page, which is forced to join such a Scriptlet. <% @ Taglib Uri = "Struts-HTML" prefix = "html"%> <% deleteParams.put ("id", CD. getId ()); deleteParams.put ("Method", "Delete");%> delete search_results.jsp Create a HashMap and deliver two properties to this MAP. In the following sections, we will create a custom label that does not have to complete this work with Java code. Our label will define a HashMap: DELETE This will make us easily create small MAPs. This example will showcase several key concepts, including using nested tags and define Scriplet variables. First of all, I will explain how this label works. These concepts are then created in several sections and describe how to write different forms of this label to handle their body and control the execution process.

Steps to build simple labels

Page 2 (9 pages a total)

Let's create a label that defines a HashMap Scriptlet variable. To do this, the label handler interface (javax.servlet.jsp.tagext.tag) is required. Therefore, the first tab we have to create will be a simple label. This tag will instantiate a map. Developers using this label can specify the type of MAP to instantiate - HashMap, TreeMap, FasthashMap, or FastTreeMap. Fasthashmap and FastTreemap come from Jakarta Commons Collection Library (for details). Developers can also specify the range of the label - page, request, and session or application range. To build this simple label, we need to complete the following steps: Create a tag interface (accurately said to javax.servlet.jsp.tagext.tag) label handler class. Create a TLD file. Create attributes in the label handler Java class. Define the properties corresponding to the properties defined in the tag handler Java class in the TLD file. Declare the Scriptlet variable in the TLD file. Implement the dostartTAG () method. In the label processing program class, set the value to the scripTlet variables according to the attribute. If you are like me, you may read the end of the book in advance, so please see the full list of tag handler classes in the appendix to understand how this process ends. In the following sections, we will analyze the implementation of MapDefineTag and analyze how to reach this step.

Step 1: Create a label handler that implements the TAG interface

Page 3 (9 pages a total)

In order to write a label handler, a TAG interface must be implemented. As mentioned earlier, this interface is used to do not manipulate the simple label handler of its label body. Just like the J2EE API document (related to the link, please refer to the reference): TAG interface defines the basic protocol between the label handler and the JSP page implementation class. It defines the survival cycle and method calling at the beginning and end of the label. The label handler interface has the following methods:

setter methods void method role int doStartTag () throws JspException process start tag int doEndTag () throws JspException process ends tag Tag getParent () / void setParent (Tag t) get / set labels parent tag void setPageContext (PageContext pc) pageContext properties Release () release All resources Tagsupport now, do not have to directly implement TAG interfaces, in contrast, the Map-defining label will inherit the Tagsupport class. This class implements a TAG interface with meaningful default methods, thus making the development of custom labels easier (see Resources for links for Tagsupport API documents). For example, the TagSupport class defines GET / setParent () and setPageContext (), which is almost the same as all tag handles. The Get / setParent () method allows labels to nested. The Tagsupport class also defines a PageContext PageContext that can be used by subclasses. This variable is set by the setPageContext () method. By default, tagsupport implements dostartTAG () to returns to Skip_body constants, indicating that the label body is not judged. In addition, by default, the DOENDTAG () method returns EVAL_PAGE, which indicates that the JSP runtime engine should be judged by the rest of the page. Finally, Tagsupport implemented Release (), which sets PageContext and its parent element as NULL. The Tagsupport class also implements the ITERATIONTAG interface and DOAFTERBODY () so that it returns Skip_body. I will explain more detailed explanation when discussing an iterative label (see Custom Label Control Process). Ok, now let us implement TAG interface by inheriting tagsupport: ... import javax.servlet.jsp.tagext.tagsupport; ... public class mapdefinetag extends tagsupport {... we have defined label handler, now need Increase the mapping of the label from the handler to the TLD file. We will process this in the next section. Then, the remaining code remaining in the mapDefinetag will be completed. Step 2: Create a TLD file

Page 4 (9 pages a total)

The TLD file is a role in the custom label handler like the WEB deployment descriptor to servlet. The TLD file lists the mappings from the label name to the label handler. Most of this file is used when the JSP page is converted. TLD files typically save the web-INF directory of the web application and declare in the web.xml file. They are generally ended with .TLD extensions. The TLD file has a introduction, identifies the version of the JSP technology and the label library used here. This introduction usually looks like this: 1.0 1.2 Map Let's analyze these tags more detailed: The root element of the TLD file is Taglib. Taglib describes a label library - a set of labels / tag handler pairs. Because we use the JSP version 1.2, you need a Tlib-Version and Short-Name elements in this example. The TLIB-VERSION element corresponds to the label version. JSP-Version corresponds to the version of JSP technology dependent on the label library. The short-name element defines the simple name of the label library that IDE and other development tools. The taglib element contains many TAG elements, and each tag in the label library has a TAG element. Because we have just created your own class, we will continue to do it, declare this class in the TLD file, as shown below: ... mapdefine Trivera.tags.map.mapdefinetag JSP ... tag element is used to map custom labels to their custom label handlers. The TAG element in the above list maps the custom tag mapDefine to the handler Trivera.tags.map.mapdefinetag. Therefore, what conversion engine running on the mapDefine is called Trivera.tags.map.mapDefinetag. Tags have been defined in TLD, followed by defining some properties of this tag in the tag handler class. (Endlessly)

转载请注明原文地址:https://www.9cbs.com/read-11714.html

New Post(0)