JSP custom tag syntax

Source: Internet
Author: User

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 >

 

 

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.