1. Create a helper class to implement the Tag function. The function is to change the color of the subject text. This class can inherit from TagSupport or BodyTagSupport. The difference between the two is that the former applies to labels without a subject, and the latter applies to labels with a subject. If you choose to inherit TagSupport, You can implement the doStartTag and doEndTag methods to implement the Tag function. If you choose to inherit BodyTagSupport, You can implement the doAfterBody method. The core code is as follows: import java. io. IOException;
Import javax. servlet. jsp. JspWriter;
Import javax. servlet. jsp. tagext .*;
/***//**
*
* @ Author Administrator
*/
Public class MessageWriterTagHandler extends BodyTagSupport ...{
// Font color attribute of the subject
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 ("
Sb. append (_ fontColor );
Sb. append (""> ");
Sb. append (bc. getString ());
Sb. append ("");
Out. print (sb. toString ());
} Catch (IOException ex )...{
// Ex. printStackTrace ();
}
Return EVAL_PAGE;
}
}
2. Compile the. tld file, which is the tag description. It must be noted that the required of the tag attribute should be set to true. Otherwise, the tag function may be unavailable. I still don't know the specific reason. The code for tld is as follows:
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
1.0
Writer
Tags/MessageWriter
Write
Com. lilijun. tags. MessageWriterTagHandler
JSP
FontColor
True >