Customizing JSP Tags

Source: Internet
Author: User
Tags tld

1. Create a new class that inherits from TagSupport, Bodytagsupport, or implements the tag interface

Corresponds to a JSP tag
public class MyTag extends TagSupport { private jspwriter writer = null;
The property corresponding to the JSP tag is private String showmsg;
Encounters < Calls
Legal return value Eval_body_include (display tag body contents) and Skip_body (do not display tag body contents) @Override public int doStartTag () throws jspexception { return Super.dostarttag (); }
Encounter > Call//Legal return value eval_page (continue processing subsequent tags) with skip_page (do not process subsequent emoticons) @Override public int Doendtag () throws jspexception {
Get PageContext object writer = This.pageContext.getOut (); try{ Writer.write (showmeg); } catch (IOException IE) { ie.printstacktrace (); } return Super.doendtag (); }
Setter method must be provided public void Setshowmsg (String showmeg) { this.showmeg = Showmeg; }}

2. Create a new Myxxx.tld file, placed in the Web-inf directory/its subdirectories, for mapping relationships

<?xml version= "1.0" encoding= "iso-8859-1"? ><taglib xmlns= "Http://java.sun.com/xml/ns/javaee"        xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance"        xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd "        version=" 2.1 ">    <tlib-version>1.0< /tlib-version>    <short-name>myshortname</short-name>    <uri>http://mycompany.com< /uri>
<!--add a custom label definition- <tag> <name>my1</name> <tag-class>tag. mytag</tag-class> <body-content>JSP</body-content> <attribute> <name >showMsg</name>
<!--configuration Required-- <required>true</required> </attribute> </tag></ Taglib>

3. Configure the tag library URI in the Web. xml file

<jsp-config>        <taglib>            <taglib-uri>http://mycompany.com</taglib-uri>            < Taglib-location>/web-inf/mytag.tld</taglib-location>        </taglib>
</jsp-config>

4. Use: Introduce taglib, then <pre:my showmsg= "xxx"/>,pre for the tag library, showmsg corresponds to the custom class, value is the instance domain of the custom class.

Customizing JSP Tags

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.