Taglib Principle and realization

Source: Internet
Author: User
Tags empty string tag name version tld

What exactly is tag? How to implement a tag?       a tag is an ordinary Java class, and the only special thing is that it must inherit TagSupport or Bodytagsupport classes. These two classes provide methods that are responsible for the interaction between the JSP page and the class you write, such as input, output. These two classes are provided by the JSP container and need not be implemented by the developer themselves. In other words, you just have to inherit the class that implements the business logic TagSupport or Bodytagsupport, and do some special work, your class is a tag. And it is responsible for the interaction with the JSP page, do not worry about you.      "Special work" usually has the following steps:     1 provides a set method for the property, which can then be set on the JSP page. Take the JSTL tag as an example c:out value= ""/, this value is the entry between the JSP data and the tag. So tag must have a SetValue method, the specific properties can not be called value. For example SetValue (String data) {this.data = data;} The name of the      "value" is defined in the TLD. What name can be taken, just tag to provide the appropriate set method can be. &NBSP;&NBSP;&NBSP;&NBSP;2) to handle doStartTag or Doendtag. These two methods are provided by TagSupport. Or take the c:out value= ""/as an example, when the JSP resolves the label, the doStartTag event is triggered at "<", and the Doendtag event is triggered when ">". Typically, logical operations are performed in the doStartTag and the output is controlled in the Doendtag.     3) to write TLD files. &NBSP;&NBSP;&NBSP;&NBSP;4) to import the TLD on the JSP page. This way, your JSP page will be able to use your own tag.      usually you will find that most of your activities are concentrated in doStartTag or Doendtag methods. Indeed, it is easy to write taglib after familiarity with some interfaces and classes. As the author of JSP Design says: The logic inside is a little more complicated, but it's not as hard as the rockets.      A simple example: Outputtag

Package Diegoyun;import Javax.servlet.jsp.jspexception;import Javax.servlet.jsp.jspwriter;import javax.servlet.jsp.tagext.tagsupport;/** * @author Chenys */public class Outputtag extends tagsupport{private String name =null; public void SetName (String name) {this.name = name.} public int doStartTag () throws jspexception{try {jspwriter out = Pagecontext.getout (); Out.print ("hello!" + name); catch (Exception e) {throw new Jspexception (e);} return eval_page; }}
Brief description: 1 How to Output to JSP page: Call JspWriter jspwriter out = Pagecontext.getout (); Out.print ...    Just remember this method. 2 after the output is processed, the function returns one of several values. Eval_page said tag has finished processing, returned to the JSP page.     There are several values, such as Eval_body_again and Eval_body_include, which we will discuss later. Writing TLD
<?xml version= "1.0" encoding= "iso-8859-1"? ><! DOCTYPE taglib Public "-//sun Microsystems, Inc.//dtd JSP Tag Library 1.2//en" "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>diego</short-name> <!--outputtag--> <tag> <name>out </name> <tag-class>diegoyun. outputtag</tag-class> <body-content>empty</body-content> <attribute> <name>name< /name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag></taglib>
Under Web-inf, create a new TLDs folder, name the file Diego.tld, and put it under the TLDs folder. The path should be like this: Web-inf\tlds\diego.tld a simple description of TLD: The name of the Short-name:taglib, also known as the prefix. For example, "C:out value=" "/" in the "C" name:tag name. For example, "C:out value=" "/" in "Out", our class also named out, because there is a prefix to distinguish, will not confuse Tag-class: the specific tag class. With package name Body-content: Refers to the contents between the tags. For example, C:out value= "" ... between the start and Close tabs is body-content. Because there is no processing body-content, the above is set to the name: attribute in empty. For example the value of C:out value= ""/.    The name can be arbitrarily taken, as long as the corresponding set method is provided in the class.    Required: Required attribute. Rtexprvalue: The Run-time expression value is supported. This is the powerful function of tag. We'll discuss it later. Temporarily set to False to write a JSP page
<%@ page language= "java"%><%@ taglib uri= "/web-inf/tlds/diego.tld" prefix= "Diego"%>My programming environment is eclipse+tomcat. Start the server, and if you follow the steps above, you can see Test tag:hello! Diegoyun words. The simplest tag just came out. It's not hard, is it? (T111)

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.