Some time ago to see some of the application of taglib, feeling very mysterious, just at the beginning of the time still do not understand what the role of Taglib, in the library several times to find out, finally understand, today to learn the preparation and configuration of taglib, now write the summary of today's study.
The main role of Taglib is to encapsulate code snippets that need to be reused and to set the properties that the code snippet might use to improve the utilization of the code. The taglib consists of three parts: A. java file that implements code snippets, a tag library description file, a TLD; The configuration of the Web.xml.
The following is the first introduction to the label implementation file, here the Decortag.java implementation of the label as an example:
Decortag.java
/*
* Decortag.java
*
* Created on March 17, 2007, 8:25
*/
Package com.wlmzfx.servlet;
Import javax.servlet.jsp.*;//jsp Class
Import javax.servlet.jsp.tagext.*;//Tag Class Library
Import java.io.IOException;
Here to implement an HTML table to display a decorative box
public class Decortag extends tagsupport{
These fields are used to control the appearance
String align;//Alignment
String title;//Title
String titlecolor;//Title foreground color
String titlealign;//title Alignment
String color;//Box Background color
String bordercolor;//Border Color
String margin;//The pixel between the border and the content
String borderwidth;//The pixel value of the border width
The following methods are necessary to set various property values, which are converted to method calls when the JSP page uses labels
public void Setalign (String value) {
This.align = value;
}
public void Settitle (String value) {
This.title = value;
}
public void Settitlecolor (String value) {
This.titlecolor = value;
}
public void Settitlealign (String value) {
This.titlealign = value;
}
public void SetColor (String value) {
This.color = value;
}
public void setBorderColor (String value) {
This.bordercolor = value;
}
public void SetMargin (String value) {
This.margin = value;
}
public void Setborderwidth (String value) {
This.borderwidth = value;
}
public void Setpagecontext (PageContext context) {
To save the page environment object as a superclass, the following tag () is used, which is important
Super.setpagecontext (context);
Set the default value for a property
align = "cente";
title = null;
Titlecolor = "White";
Titlealign = "Left";
color = "LightBlue";
BorderColor = "BLACK";
margin = "20″;
BorderWidth = "4″;
}
/**
* When this method encounters the <decor:box> tag again, it calls
**/
public int doStartTag () throws jspexception{
try{
Gets the output stream from the PageContext object and passes it to the Setpagecontext () method
JspWriter out = Pagecontext.getout ();
Out.print ("<div align=" "+align+" ' > "+" <table bgcolor= ' "+bordercolor+" ' "+" <border= ' 0′cellspacing= ' 0′ ' "+" "Cellpadding= '" +borderwidth+ "" ");
if (title!= null)
Out.print ("<tr><td align=" "+titlealign+" ' > "+" <font face= ' Helvetica ' size= ' "+" + "+1′" "+ titlecolor+ "' ><br>" +title+ "</b></font></td><td>");
Out.print ("<tr><td<table bgcolor=" "+color+" "" + "border= ' 0′cellspacing= ' 0′" + "cellpadding=" "+margin+" " ><tr><td> ");
}catch (IOException e) {
throw new Jspexception (E.getmessage ());
}
The return value tells the JSP class to handle the label theme
return eval_body_include;
}
Called when a </decor:box> end tag is encountered
public int Doendtag () throws jspexception{
try{
JspWriter out = Pagecontext.getout ();
Out.println ("</td></tr></table><td><tr><table></div>");
}catch (IOException e) {
throw new Jspexception (E.getmessage ());
}
Returns a value indicating that the JSP page continues to be processed
return eval_page;
}
}
Decor_1_0.tld (Put in Web-inf/tids/decor_1_0.tld
In general, the prefix _1_0 to represent the version of the TLD file, which is actually an XML file
<?xml version= "1.0″encoding=" utf-8″?>
<taglib version= "2.0″xmlns=" Http://java.sun.com/xml/ns/j2ee "xmlns:xsi=" http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd ">
<tlib-version>1.0</tlib-version><!– file Version –>
<short-name>decor</short-name><!– Library name –>
<uri>http://www.wlmzfx.com/tlds/decor_1_0.tld</uri><!– Unique identifier –>
<tag><!– first defines a label for a tag library –>
<name>box</name><!– first defines the label name and implements the class –>
<tagclass>com.wlmzfx.servlet.DecorTag</tagclass>
<!– defines each property of a tag library –>
<attribute>
<name>align</name><!– Property –>
<required>false</required><!– not have to –>
<rtexprvalue>true</rtexprvalue><!– should have <%=%> value –>
</attribute>
<attribute>
<name>color</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>bordercolor</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>margin</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>borderWidth</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>title</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>titleColor</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>titleAlign</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!–a validator verifies that's tags are used correctly at JSP
Translation time. Validator entries look like this:
<validator>
<validator-class>com.mycompany.TagLibValidator</validator-class>
<init-param>
<param-name>parameter</param-name>
<param-value>value</param-value>
</init-param>
</validator>
–>
<!–a Tag Library can register Servlet context event listeners in
Case it needs to react to such events. Listener entries Look
Like this:
<listener>
<listener-class>com.mycompany.TagLibListener</listener-class>
</listener>
–>
</taglib>
You then need to configure the label in Web.xml to add the following in Web.xml:
<taglib>
<!– when you see the tag library unique identifier –>
<taglib-uri>http://www.wlmzfx.com/tlds/decor_1_0.tld</taglib-uri>
<!– uses the tag library to describe the local copy of the file –>
<taglib-location>tlds/decor_1_0.tld</taglib-location>
</taglib>
Here is an example to illustrate the use of this tag library:
Add the following code to any JSP page:
<!– here only defines the Title,color,margin attribute –>
<decor:box title= "LogOut was done" color= "Red" margin= "100″>
<!– here to add the contents of the table, you can continue to use the <decor:box> tag, but also must end with </decor:box>-
content here
</decor:box>