Java JSP Custom Tags

Source: Internet
Author: User
Tags tld

Custom Label Development Steps

1) Write a generic Java class that inherits the Simpletagsupport class, called the tag processor class

 PackageGz.itcast;Importjava.io.IOException;Importjavax.servlet.jsp.JspException;ImportJavax.servlet.jsp.tagext.SimpleTagSupport; Public classIftagextendsSimpletagsupport {Private  Booleantest; Public voidSettest (Booleantest) {     This. Test =test;} Public voidDotag ()throwsjspexception, IOException {//TODO auto-generated Method Stub    if(test) { This. Getjspbody (). Invoke (NULL); }}}

2) Create a itcast.tld file in the Web-inf directory of the Web project, called the tag library's declaration file. (Refer to the TLD file of the core tag library)

<?xml version= "1.0" encoding= "UTF-8"? ><taglib xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"version= "2.1" ><tlib-version>1.1</tlib-version>< Short-name>itcast</ Short-name><uri>http://gz.itcast.cn</uri><tag><name>if</name><tag-class>gz.itcast.iftag</tag-class><body-content>scriptless</body-content><attribute><name>test</name>< Required>true</required><rtexprvalue>true</rtexprvalue></attribute></tag></taglib>

3) Import the custom tag library in the header of the JSP page

<% @taglib uri="http://gz.itcast.cn" prefix="Itcast"%>

4) Use a custom label in the JSP

Import= "java.util.*" pageencoding= "Utf-8"%><% @taglib uri= "http://gz.itcast.cn" prefix= "Itcast" %><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >if test= "${10>5}" > Condition established </itcast:if>  </body> 

Java JSP Custom Tags

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.