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