1. Define the label class.
Label extension bodytagsupport class.
Code
Package Testtag;
ImportJava. Io. ioexception;
Import javax. servlet. HTTP. httpservletrequest;
Import javax. servlet. JSP. jspexception;
Import javax. servlet. JSP. jsptagexception;
Import javax. servlet. JSP. jspwriter;
Import javax. servlet. JSP. tagext. bodytagsupport;
Import javax. servlet. JSP. tagext. tagsupport;
Public ClassLinktagExtendsBodytagsupport {
Public Linktag (){
// Todo auto-generated constructor stub
}
Private String href = "" ;
Public String gethref (){
Return Href;
}
Public VoidSethref (string href ){
This. Href=Href;
}
Public Int Dostarttag () Throws Jsptagexception {
Return Eval_body_buffered;
}
Public Int Doendtag () Throws Jsptagexception {
String body = This . Getbodycontent (). getstring ();
Httpservletrequest request = (Httpservletrequest) pagecontext. getrequest ();
Try {
Jspwriter writer = Pagecontext. getout ();
String Str = " <A href =' " + This . Href + " '> " + Body + " </A> " ;
Pagecontext. getout (). Print (STR );
}
Catch (Ioexception e ){
Throw New Jsptagexception (E. getmessage ());
}
Return Skip_body;
}
}
2. Define the Tag file mytaglib. TLD.
Code
<? XML version = "1.0" encoding = "ISO-8859-1" ?>
< Taglib >
< Tlib-version > 1.0 </ Tlib-version >
< JSP-version > 1.2 </ JSP-version >
< Tag >
< Name > Linktag </ Name >
< Tag-class > Testtag. linktag </ Tag-class >
< Body-content > JSP </ Body-content >
< Attribute >
< Name > Href </ Name >
< Required > True </ Required >
< Rtexprvalue > True </ Rtexprvalue >
</ Attribute >
</ Tag >
</Taglib>
3. Add reference in Web. xml.
< JSP-config >
< Taglib >
< Taglib-Uri > Mytaglib </ Taglib-Uri >
< Taglib-location > /WEB-INF/mytaglib. TLD </ Taglib-location >
</ Taglib >
</ JSP-config >
4. Call custom tags on the page.
<% @ Taglib URI = " Mytaglib " Prefix = " Mytag " %>
< Mytag: linktag Href = "Http://www.baidu.com" > Test tag </ Mytag: linktag >