Use of TLD tags (custom tags)

Source: Internet
Author: User
Tags tld

1. Create a new tag named name. TLD. Note that this file should be placed under the WEB-INF file (same as Web. XML) and will be automatically loaded at runtime

<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype taglib public "-// Sun Microsystems, Inc. // dtd jsp tag library 1.2 // "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version> 1.0 </tlib-version> <JSP-version> 1.2 </JSP- version> <short-Name> name </short-Name> <URI> http://www.yuqiaotech.com/name </uri> <description> name tag library </description> <tag> <Name> getname </Name> <tag-class> COM. yuqiaotech. PMS. webapp. tags. getname </Tag-class> <attribute> <Name> username </Name> <required> true </required> <rtexprvalue> true </rtexprvalue> </attribute> <attribute> <Name> var </Name> <rtexprvalue> false </rtexprvalue> </attribute> </Tag> </taglib>

2. Now we start to implement the getname method of the tag. Create a new package com. yuqiaotech. PMS. webapp. tags, which contains two files: getname. Java and pmstag. java.

Getname. Java

Package COM. yuqiaotech. PMS. webapp. tags; import javax. servlet. JSP. jspexception; public class getname extends pmstag {private string username; Public String GetUserName () {return username;} public void setusername (string username) {This. username = username;} Public String getscope () {return scope;} public void setscope (string scope) {This. scope = scope;} private string scope; Public int dostarttag () throws jspexception {// assign the value to string myname = username + "------ data obtained by Tag "; setattribute (VAR, this, pagecontext, scope, myname); return Super. dostarttag ();}}

Pmstag. Java note that pmstag inherits the struts custom tag library tagsupport

package com.yuqiaotech.pms.webapp.tags;import javax.servlet.jsp.PageContext;import javax.servlet.jsp.tagext.TagSupport;public class PmsTag extends TagSupport{protected String var;    public static void setAttribute(String attrName,TagSupport tag,PageContext pageContext,String scope,Object obj){    if (scope != null) {            if (scope.equals("page")) {                pageContext.setAttribute(attrName, obj);            } else if (scope.equals("request")) {                pageContext.getRequest().setAttribute(attrName, obj);            } else if (scope.equals("session")) {                pageContext.getSession().setAttribute(attrName, obj);            } else if (scope.equals("application")) {                pageContext.getServletContext().setAttribute(attrName, obj);            } else {                throw new RuntimeException("Attribute 'scope' must be: page, request, session or application :"+scope);            }        }else{        pageContext.getRequest().setAttribute(attrName, obj);        }    }public String getVar() {return var;}public void setVar(String var) {this.var = var;}}

4. Write a JSP page note added in querybooks. jsp, the project can see my article: Eclipse new struts engineering http://blog.csdn.net/b10090120/article/details/8045271

<% @ Page Language = "Java" pageencoding = "UTF-8" %> <% @ taglib prefix = "S" uri = "/Struts-tags" %> <% @ taglib URI = "http://www.yuqiaotech.com/name" prefix = "name" %> <HTML> 

5. The running effect is as follows:

Can everyone in http://pan.baidu.com/share/link? Upload id = 77616 & UK = 1763003608 download source file

Description of some attributes of TLD tags

The tag description file is an XML document that describes the tag information of the entire tag library, each Tag Processor in the tag library, and its attributes. It can contain the following elements.

1. tlibversion: the version number of the tag library, which is a point-type decimal number (for example, 1.0). It consists of up to four groups of digits separated by decimal places.

2. jspversion: the minimum version of the JSP specification required by the tag library, such as jsp1.1.

3. shortname: abbreviation of the tag library. jsp can use this name as the default prefix of the tag in the library.

4. Uri: the unique URI element of the tag library.

5. Info: Tag library description.

6. Tag: Add tags to describe each tag of the library.

The tag element contains information about tags and their attributes:

1. Name: the name of the tag used with the name prefix of the tag library. It is the unique tag ID of the JSP Container.

2. tagclass: The full name of the tag processor class that implements tags.

3. teiclass: The full name of the tag additional information (TEI) class. The Tei class provides information about the variable creation and validity verification of the tag processor.

4. bodycontent: describes how the tag processor uses the content of the TAG body. There are three values:

L empty: indicates that the label body must be empty;

L JSP: the script element is evaluated like the template and other labels.

L tagdependent: The content is unblocked and written to bodycontent. Other script elements are in the source code form and are not interpreted by the JSP Container.

5. Info: descriptive information of the tag.

6. attribute: the attribute information encoded when tags are used to define the attributes of tags.

The attribute element can contain the following elements:

1. Name: name of the attribute.

2. Required: required or not.

3. rtexprvalue: Can the attribute value be specified using an expression?

7. variable element
 
1. Name-given: variable name constant

2. Name-from-attribute: name of an attribute. The value of translation-time during conversion gives the attribute name.

Must have one of name-given or name-from-attribute. The sub-element of the lower column is optional.

3. The fully qualified name of Variable-class-variable. The default value is Java. Lang. String.

4. Check whether the declare variable references the new object. The default value is true.

5. Scope-Scope of the script variable defined. The default value is nested in the following table:
 
 
Nested: between the start and end tags, In the doinitbody and doafterbody methods that implement the bodytag tag handler, otherwise, in the dostarttag
 
At_begin: From the start tag to the end of the page, In the doinitbody and doafterbody methods that implement the bodytag tag handler, otherwise in the dostarttag and doendtag
 
At_end: After the end tag ends until the end of the page, in the doendtag

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.