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