1. First create a helper class that implements the tag function, which is to change the color of the body text. This class can inherit TagSupport or Bodytagsupport, and the difference is that the former applies to labels that have no principals, and the latter to the subject. If you choose to inherit TagSupport, you can implement the doStartTag and Doendtag two methods to implement the function of tag, if you choose to inherit Bodytagsupport, you can implement Doafterbody this method. The core code looks like this:
Import java.io.IOException;
Import Javax.servlet.jsp.JspWriter;
Import javax.servlet.jsp.tagext.*;
/** *//**
*
* @author Administrator
*/
public class Messagewritertaghandler extends Bodytagsupport ... {
Font Color properties for body
protected String _fontcolor = "BLACK";
/** *//** Creates a new instance of Messagewritertaghandler * *
Public Messagewritertaghandler () ... {
}
Public String Getfontcolor () ... {
return _fontcolor;
}
public void Setfontcolor (String fontcolor) ... {
_fontcolor = FontColor;
}
public int doafterbody () ... {
bodycontent BC = this.getbodycontent ();
JspWriter out = Bc.getenclosingwriter ();
StringBuffer sb = new StringBuffer ();
Try ... {
Sb.append ("<font color=");
Sb.append (_fontcolor);
Sb.append (">");
Sb.append (Bc.getstring ());
Sb.append ("</font>");
Out.print (Sb.tostring ());
catch (IOException ex) ... {
Ex.printstacktrace ();
}
return eval_page;
}
}
2. To write a. tld file, the file is the description of tag, need to explain the tag attribute of the required to be set to true, otherwise the label function may not be used. I don't know the exact reason. The code for the TLD looks like this:
<?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>
<short-name>writer</short-name>
<uri>tags/MessageWriter</uri>
<tag>
<name>write</name>
<tag-class>com.lilijun.tags.MessageWriterTagHandler</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>fontColor</name>
<required>true</required>>
</attribute>
</tag>
</taglib>