Package Jstl Custom Labels yourself

Source: Internet
Author: User

The first step is to do a class that derives from the Simpletagsupport, "alt+/" option to override the Dotag () method.

 Public classTesttagextendsSimpletagsupport {PrivateString outertagname= "P";  Public voidsetoutertagname (String outertagname) { This. Outertagname =Outertagname; }    Private intcount;  Public voidSetCount (intcount) {         This. Count =count; } @Override Public voidDotag ()throwsjspexception, IOException {jspfragment Frag= This. Getjspbody ();//the currently generated fragment         This. Getjspcontext (). Getout (). Write ("<" +outertagname+ ">");//Added Properties         for(inti=0;i<count;i++) {Frag.invoke (NULL);//Build Output        }         This. Getjspcontext (). Getout (). Write ("</" +outertagname+ ">"); }}

Step Two, configure

Create a new Xml.file file in the Web-inf folder, change the extension to TLD, select the XML template, and complete the creation.

Taglib as the root element

<taglib xmlns= "Http://"    xmlns:xsi= " Xmlschema-instance "    xsi:schemalocation=" Http:// Ns/javaee/web-jsptaglibrary_2_1.xsd "    version=" 2.1 ">

A description of the entire file is written under the root element

<description> Custom Jstl Tags </description> description
<display-name>jstl Custom Labels </display-name> display names
<tlib-version>1.1</tlib-version> version
<short-name>c</short-name> prefixes
<uri></uri> Unique identification

  <description> Customize Jstl Tags </description>   <display-name>jstl custom Labels </display-name>  <tlib-version>1.1</tlib-version>  <short-name>c</short-name>  <uri></uri>

Back write <tag> Label

<name>catch</name> quote: That's the name behind it.
<tag-class>com.itnba.maya.myel.TestTag</tag-class> Com.itnba.maya.myel is the first step to do the package name Testtag is the class name
<body-content>scriptless</body-content> cannot write scripts, i.e. cannot write <%%>
<required>false</required> is required, true is auto generate must fill, false can write not write
<rtexprvalue>false</rtexprvalue> can I use El expressions <tag>

<tag> <description>..... Description</description> <name>Catch</name> <tag-class>com.itnba.maya.myel.testtag</tag-class> <body-content>scriptless</body-content> <attribute> <description>First Property</description> <name>outerTagName</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <description>Second Property</description> <name>count</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag></taglib>

In the third step, you can now refer to this JSTL custom label.

Pilot the El function written,

Prefix is the name used in ${}, and the URI is the unique identity in the second-step configuration

<%@ taglib prefix= "C" uri= "Http://"%>

Use the prefix name:<tag> in <name>, and then you can add the written property

<c:catch outertagname= "H3" count= "2" >Hello Lee<br></c:catch>

This code should display the number third title, Count is the number of cycles, two times, as shown.

Package Jstl Custom Labels yourself

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: 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.