To develop a custom label in a JSP:
1 · Developing custom Label Processing classes
2 · Create a *.tid file that corresponds to a tag library for each TLD tag, with multiple tags for each tag library
3 · Using tags in JSPs
Considerations for developing Custom label classes:
1 · The custom label class should inherit a parent class Javax.servlet.jsp.tagext.SimpleTagSupport
2 · If a custom label class contains attributes, each attribute should have a corresponding Getter/setter method
3 · Overriding the Dotag () method This method is responsible for generating the contents of the page
Example:
public class Helloworldtag extends simpletagsupprt{
Overriding the Dotag () method
public void Dotag () throws jspexception,ioexception{
This.getjspcontext (). Getout (). Write ("Hello World" + New Date ())
}
}
Creating TLD Labels
TLD is the abbreviation for Tag library definition
We can copy a file template into Tomcat \webapps\examples\web_inf\jsp2 contains a jsp2-example-taglib.tld
Copy the file to the Web app's Web-inf path and make a simple modification
Example:
<!--define Labels--
Root node:
Tag
Using labels
Using labels requires two points:
1 • Tag library URL determines which label is used
2 · Label name: Determine which label to use
There are two steps to using a tag library:
1 · Import Tag Library import tag library using taglib compilation directives
<%@ taglib uri= "Http://tomcat.apache.org/debug-taglib" prefix= "MyTag"%>
URI: The URI of the tag library
Prefix: Short name
2 • Use tags; use custom tags in jsps
As we set the
<body-content>TAGDEPENDENT</body-content> define the label body as empty
We just need to call:
<mytag:log/> Prefix + label signature
JSP2 Custom Labels