Two methods of implementation
(1) Create a label processor class to implement the Sampletag interface
public class Hellosimpletag implements Simpletag
Hellosimpletag.java PackageCn.stud.wlc.tag;Importjava.io.IOException;ImportJavax.servlet.jsp.JspContext;Importjavax.servlet.jsp.JspException;ImportJavax.servlet.jsp.PageContext;Importjavax.servlet.jsp.tagext.JspFragment;ImportJavax.servlet.jsp.tagext.JspTag;ImportJavax.servlet.jsp.tagext.SimpleTag; Public classHellosimpletagImplements simpletag {
Setting properties
private String value;
Private String count;
Private PageContext PageContext;
The set method is required for these properties to be described in the TLD file
Public voidSetValue (String value) { This. Value =value; } Public voidSetCount (String count) { This. Count =count; } Public voidDotag ()throwsjspexception, IOException {//TODO auto-generated Method Stub//the execution of the tag body logic should actually be written into the method//Eight member variables that can go back to PageContextSystem.out.println ("Value:" +value+ "Count:" +count); Pagecontext.getout (). Print ("Hello word"); } PublicJsptag getParent () {System.out.println ("GetParent"); return NULL; } Public voidsetjspbody (jspfragment arg0) {//TODO auto-generated Method StubSystem.out.println ("Setjspbody"); } Public voidSetjspcontext (Jspcontext arg0) {//TODO auto-generated Method Stub//the PageContext object of the JSP page is passed in.//JSP engine calls This. PageContext =(PageContext) arg0; System.out.println ("Setjspcontext"); } Public voidsetParent (Jsptag arg0) {//TODO auto-generated Method StubSystem.out.println ("SetParent"); }}
(2) Succession Simpletagsupport
Package Cn.stud.wlc.tag;
Import java.io.IOException;
Import javax.servlet.jsp.JspException;
Import Javax.servlet.jsp.JspWriter;
Import Javax.servlet.jsp.PageContext;
Import Javax.servlet.jsp.tagext.SimpleTagSupport;
public class Maxtag extends Simpletagsupport {
Private String NUM1;
Private String num2;
public void SetNum1 (String num1) {
THIS.NUM1 = NUM1;
}
public void setNum2 (String num2) {
this.num2 = num2;
}
you don't have to write all the ways after Dotag .
public void Dotag () throws Jspexception, IOException {
int a=0;
int b=0;
PageContext PageContext = (PageContext) getjspcontext ();
JspWriter out =pagecontext.getout ();
try {
A=integer.parseint (NUM1);
B=integer.parseint (NUM2);
Out.print (A>B?A:B);
} catch (Exception e) {
Out.print ("Incorrect input format");
}
}
}
Create an XML file for the MYTAG.TLD (Tag library profile) under Web-inf Lib
<?xml version= "1.0" encoding= "UTF-8"? ><taglib xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "http// Www.w3.org/2001/XMLSchema-instance "xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"version= "2.0" > <!--description of TLD Files--<description>there are custom tags of tag</description> <tlib-ve Rsion>1.0</tlib-version> <!--recommended tag prefixes for use in JSP pages--< Short-name>WLC</ Short-name> <!--TLD File ID-<uri>http://www.wlc.com/mytag/core</uri> <!--describe your custom Hellosimpletag label-<tag> <description>view IP of client</description> <!--the name of the label--<name>Hello</name> <!--tags are in the full class name--<tag-class>cn.stud.wlc.tag.hellosimpletag</tag-class> <!--the type of tag body--<body-content>empty</body-content> <!--describes the property value of the current label-- <attribute> <name>value</name> <required>true</required> <!--runtime expression value run-time expressions values whether the current property can accept the value of a run-time expression-<rtexprvalue& Gttrue</rtexprvalue> </attribute> <attribute> <name>count</name> <required>false</required> <!--runtime expression value run-time expressions values whether the current property can accept the value of a run-time expression-<rtexprvalue& Gtfalse</rtexprvalue> </attribute> </tag></taglib>
called in the JSP
<!--import Tag library file (custom)--
<% @taglib uri= "http://www.wlc.com/myTag/core" prefix= "wlc"%>
the specific use
<WLC:Hello value= "name" Count = "Ten"/>
Ctrl+shift+o Remove Excess Import
Jstl Custom Labels