JSP custom tag

Source: Internet
Author: User
Tags tld

JSP custom tag

Custom tags are custom JSP language elements. When a JSP page contains a custom tag, it is converted into a servlet, And the tag is converted into an operation on the object called tag handler, that is, the Web iner calls those operations when the servlet executes.

JSP tag extension allows you to create new tags and insert them directly to a JSP page. The Simple Tag Handlers is introduced in the JSP 2.0 specification to write these custom tags.

Custom tags without tags

First, you can inherit the SimpleTagSupport class and override the doTag () method to develop a simple custom tag.
Note that the Selvlet class here cannot be a native class, that is, it must be in the package, or the configuration file will not take effect later.

Import javax. servlet. jsp. jspException; import javax. servlet. jsp. tagext. simpleTagSupport; import java. io. IOException; import java. io. stringWriter; public class MyServlet extends SimpleTagSupport {@ Override public void doTag () throws JspException, IOException {JspWriter out = this. getJspContext (). getOut (); // get the output stream out of the current page. println ("Hello World! "); // Output Hello World! }}

Next, write a tag library under the WEB-INF directory, ending with a tld suffix, here write mytag. tld


      
          
   
    
2.0
           
           
   
    
Ex
           
               
                
    
     
Hello
                
                
    
     
Com. mytag. MyServlet
                
                
    
     
Empty
            
       
  

The following is the jsp page for testing.

<% @ Page contentType = "text/html; charset = UTF-8 "language =" java "%> <% -- Here prefix is the tag prefix name uri is the tag library address -- %> <% @ taglib prefix =" ex "uri =" /WEB-INF/myTag. tld "%>
<% -- Use custom tags -- %>

In this way, the page will output Hello World! Now

Access the custom tag body

Access the TAG body. In this way, you need to modify the above three files.
The Selvlet must contain code for obtaining the TAG body.

Import javax. servlet. jsp. jspException; import javax. servlet. jsp. tagext. simpleTagSupport; import java. io. IOException; import java. io. stringWriter; public class MyServlet extends SimpleTagSupport {@ Override public void doTag () throws JspException, IOException {StringWriter sw = new StringWriter (); // string output stream, wrap a string getJspBody (). invoke (sw); // put the label body content into the sw stream. getJspContext (). getOut (). println (sw); // output to the page }}

In mytag. tld, set the TAG body not to be empty.

<code class=" hljs xml"><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%2D%2D%3E-->    <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">        <tlib-version>2.0</tlib-version>        <!--{cke_protected}{C}%3C!%2D%2D%E6%8C%87%E5%AE%9A%E5%89%8D%E7%BC%80%E5%90%8D%2C%E5%8F%AF%E4%BB%A5%E5%9C%A8jsp%E5%A3%B0%E6%98%8E%E4%B8%AD%E6%9B%B4%E6%94%B9%2D%2D%3E-->        <short-name>ex</short-name>        <tag>            <!--{cke_protected}{C}%3C!%2D%2D%E6%8C%87%E5%AE%9A%E6%A0%87%E7%AD%BE%E5%90%8D%E7%A7%B0%2D%2D%3E-->            <name>Hello</name>            <!--{cke_protected}{C}%3C!%2D%2D%E6%8C%87%E5%AE%9A%E9%82%A3%E4%B8%AAServlet%E6%89%A7%E8%A1%8C%E8%BF%99%E4%B8%AA%E6%A0%87%E7%AD%BE%E8%A7%A3%E9%87%8A%2D%2D%3E-->            <tag-class>com.mytag.MyServlet</tag-class>            <!--{cke_protected}{C}%3C!%2D%2D%E8%AE%BE%E7%BD%AE%E6%A0%87%E7%AD%BE%E4%BD%93%E4%B8%BA%E7%A9%BA%2D%2D%3E-->            <body-content>scriptless</body-content>        </tag>    </taglib></code>

Finally, the jsp page for testing

<% @ Page contentType = "text/html; charset = UTF-8 "language =" java "%> <% -- Here prefix is the tag prefix name uri is the tag library address -- %> <% @ taglib prefix =" ex "uri =" /WEB-INF/myTag. tld "%>
<% -- Use custom tags -- %> Hahahhahahhah

The hahahhahahhah

In addition, custom tag attributes are not frequently used, so we will not detail them here.

Related Article

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.