How Do I: Create Custom Tags from Scratch?

In JSP it is possible to define your own custom HTML tags. This topic describes the specifics of creating a custom tag within WebLogic Workshop. However, it does not discuss custom tag creation itself. For detailed information on creating custom JSP tags, see your favorite JSP documentation. To examine a sample that uses custom tags, see CustomTags.jsp Sample.

To write a custom tag, you must:

  1. Create a tag handler file. This file contains the tag handler class that will be executed when the JSP processor encounters your custom tag on a JSP page.
  2. Create a tag library descriptor (TLD) file. The tag library descriptor file is an XML file that tells the JSP processor where to find the tag handler class once it encounters your custom tag on a JSP page.
  3. (Optionally) Edit the file web.xml to make your custom tags available in a JSP's Palette window.

If you have a set of custom tags that you want to use in multiple applications, you might want to build a JAR library.

Location of the Tag Handler File

The tag handler class contains the Java code that is executed whenever the JSP engine encounters your custom tag on a JSP page. The tag handler class extends javax.servlet.jsp.tagext.TagSupport, and contains variables and methods that handle the various parts of an HTML tag, such as doStartTag and doEndTag.

In WebLogic Workshop, the tag handler file must be located in WEB-INF/src. For example, in the sample mentioned above, various tag handler files are located in WEB-INF/src/tag_handlers.

Location of the Tag Library Descriptor File

The tag library descriptor (TLD) file tells the JSP processor where to find the tag handler class. A JSP page that uses these custom actions references this file, as in this example:

<%@taglib prefix="custom" uri="/WEB-INF/taglib.tld"%>

As the example shows, the TLD file should be located in the WEB-INF folder.

Editing the file web.xml

When you edit the web.xml file and define the tag library descriptor file, the custom tags will appear in the Palette window when you are working with a JSP in your web project. In the file web.xml, locate the definition of the three standard NetUI tag library TLD files, and define your custom tag library descriptor immediately after it. An example of this definition is show in the following example:

<taglib>
    <taglib-uri>taglib.tld</taglib-uri>
    <taglib-location>/WEB-INF/taglib.tld</taglib-location>
</taglib>

If you now open a JSP and scroll to the bottom of the Palette window, you will notice the header CustomLibrary (taglib.tld) followed by the custom tags you defined.

Creating a custom tag JAR

To create a JAR library containing custom tags, you should package the tag handle files and the tag library descriptor file in a Java project. For general information on re-using Java code in a Java project, see How Do I: Reuse Existing Java Code?

To build the custom tag JAR

  1. Create a Java project.
  2. Create a source folder for your tag handler files and create the tag handler files in this folder.
  3. In the Application window, right-click the Java project folder and select Properties. The Project Properties dialog appears.
  4. In the left-hand pane, make sure that Paths is selected. In the right-hand pane, click Add Path in the Source Path section. The Select Directory dialog appears.
  5. Locate and select the source folder with your tag handler files, and click Select Directory. Then, in the Project Properties dialog, click OK.
  6. Create the folder META-INF and create the tag library descriptor (TLD) file in this folder
  7. Build the JAR. The JAR will be stored in your application's Libraries folder in the IDE, which corresponds to the APP-INF/lib folder in the root of your application folder on your computer's file system.

To build a custom tag JAR in a Web Project

  1. In the IDE's Application window, right-click the folder WEB-INF/lib located in your web project folder, and select Import. The Import Files dialog appears. Locate the custom tag JAR file and click Import.
  2. In a JSP that uses the custom tags, reference the JAR file, as shown in this example:


    <%@taglib prefix="custom" uri="/WEB-INF/lib/mytags.jar"%>

  3. In the web project's web.xml file, reference the JAR file, as shown in the following example, after the definition of the three standard NetUI tag library TLD files:


    <taglib>
        <taglib-uri>taglib.tld</taglib-uri>
        <taglib-location>/WEB-INF/lib/mytags.jar</taglib-location>
    </taglib>

Related Topics

CustomTags.jsp Sample

How Do I: Reuse Existing Java Code?