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 ("

Sb.append (_fontcolor);

Sb.append (">");

Sb.append (Bc.getstring ());

sb.append ("");

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:

  

  

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 >

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.