A simple JSP custom label

Source: Internet
Author: User
Tags opening and closing tags tld

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

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.