JSP custom tag Library Development demonstration

Source: Internet
Author: User
Tags tld

JSP custom tag Library Development demonstration, and provides a ready-made example for download.

 

Development of tag library programs in netbeans 7.0

 

1. Create a web applcation. Select New Project> Web applaction from the menu and click Next.

2. Enter the project name taghello, set a project location, and click Next.

3. Select the application server, Tomcat and glassfish.

4. Complete the project creation.

 

Add the following code:

 

Create a TLD file:

1. Choose new File> Web> tag library> descriptor from the menu, and click Next.

2. The input TLD name isTaglibrary, Other Default, complete.

 

1. Create a tag processor, choose New File> Web> tag handler from the menu, and set the class nameTaghello, Enter the package name (the package must be written; otherwise, an error occurs)Tag, And click Next step.

2. Select the TLD file just created in the TLD file, which is created in the WEB-INF/TLDs by default.

3. Click New at attributes to create a parameter
Name, complete.

 

Taghello. Java content

 

Public class myhello extends simpletagsupport

{

 

Private string name;

 

Public void setnmae (string name)

{

This. Name = Name;

}

 

Public void dotag () throws jspexception

{

Getjspcontext (). getout (). Print ("hello" + name );

}

}

 

Mark library filesTaglibrary. TLD

 

<Tag>

<Name> taghello </Name>

<Tag-class> tag. taghello </Tag-class>

<Body-content> scriptless </body-content>

<Attribute>

<Name> name </Name>

<Rtexprvalue> true </rtexprvalue>

<Type> JAVA. Lang. String </type>

</Attribute>

</Tag>

 

Page JSP file

 

<% @ Page contenttype = "text/html" pageencoding = "UTF-8" %>

<% @ Taglib prefix = "mytag" uri = "/WEB-INF/TLDs/taglibrary" %>

<! Doctype HTML>

<HTML>

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">

<Title> JSP page </title>

</Head>

<Body>

<Mytag: taghello
Name = "Dao ran Jue Kong"/>

</Body>

</Html>

 

Compilation Test

Pass the name parameter to the custom taghello tag. The parameter starts with lowercase and the following result is displayed:

 

Page display: Hello Dao ran Jue empty

 

The tag library test is successful.

 

Source code download

Http://download.csdn.net/detail/joyous/3972345

 

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.