Implementation of a simple JSP custom label development process

Source: Internet
Author: User
Tags xmlns tld stringbuffer

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>

Related Article

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.