Learn a simple JSP custom tag, followed by more examples, will be updated:
Example 1:
Step: 1. Write tag implementation class: Inherit Javax.servlet.jsp.tagext.SimpleTagSupport; Rewrite the dotag to achieve the output on the Web page; 2. In the Web-inf directory or its subdirectories, create a helloword.tld file, the description of the custom label Note: The label processing class must be placed in the package, not the naked class; no need to modify Web. xml;//tld:tag LIB Description tag library describes Java code:
Package Com.mytag; Import java.io.IOException; Import javax.servlet.jsp.JspException; Public class extends javax.servlet.jsp.tagext.simpletagsupport{ @Override publicvoid Throws jspexception, IOException { // Get the current context of this JSP file, get the output stream Getjspcontext (). Getout (). Write ("helloworld!" ); }}
JSP Code:
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><%@ taglib URI="/helloworldtaglib"prefix="MyTag"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Test your custom JSP tag tag</title></Head><Body> <Mytag:helloworld/></Body></HTML>
TLD description File:
<?XML version= "1.0" encoding= "UTF-8"?><taglibxmlns= "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"> <tlib-version>1.0</tlib-version> <Short-name>MyTag</Short-name> <URI>/helloworldtaglib</URI> <Tag> <name>HelloWorld</name> <Tag-class>Com.mytag.HelloTag</Tag-class> <body-content>Empty</body-content> </Tag> </taglib><!--Explanation: -<!--short-name: The prefix name for this tag is <mytag:/> can prevent conflicts with the names of other labels defined by others -<!--URI: The unique identifier used by the current TLD file to access it, used to find the appropriate TLD file -<!--Body-content: The information between the opening and closing tags, the label body, such as the tag body is empty -<!--when Tomcat sees the HelloWorld of <mytag:helloworld/>, it looks for Name=helloworld corresponding class under tab, calling Dotag method -
Because my TLD file here is placed under/web-inf/tags/, to configure the Web. xml file: Plus configuration:
<Jsp-config> <taglib> <!--the URI path of the tag library is the URI that is declared in the JSP header file <%@ taglib uri= "/helloworldtaglib" prefix= "MyTag"%> - <Taglib-uri>/helloworldtaglib</Taglib-uri> <taglib-location>/web-inf/tags/helloworld.tld</taglib-location> </taglib> </Jsp-config>
Output Result:
There are other uses in the back, continue to update ".....
A simple JSP custom label