Show TOC

Developing Tag FilesLocate this document in the navigation structure

Context

You use this procedure to develop tag files for your Web applications. Tag files facilitate scriptless JSP development and are easily created and deployed. You can bundle tag files into tag libraries and reuse them as Java archives (JARs). Similarly to JSPs, you can create tag files in JSP syntax and in XML syntax. Tag files written in JSP syntax have the .tag extension. Tag files written in XML syntax have the .tagx extension.

Note

If you change a tag file on the file system, the JSP document that uses it will be re-compiled on the next request to it.

Procedure


  1. To create a tag file, you create a text file with an extension .tag under WEB-INF/tags in your Web application, for example myTag.tag .

  2. To denote the file a tag file, you start the implementation with the tag directive, <%@ tag%> .

    • Optionally, you may set one or more of the attributes in the tag directive.

    Attribute

    Semantics

    Possible Values

    Default Value

    body-content

    Specifies if the tag has body content.

    empty - the tag has no body content

    scriptless - the tag has body content but no scripting elements are permitted

    tagdependent - the tag interprets the body content itself

    scriptless

    import

    Specifies Java classes available to be available in the scripting environment.

    You can specify one or more values, separated by commas. Values may be either fully-qualified class name or a Java class package with a .* suffix, so that all classes in the package are available.

     

    dynamic-attributes

    Specifies that the tag accepts dynamic attributes.

    The value of the attribute is the name of the scripting variable to hold a map of the dynamic attributes of the tag.

     
  3. To declare attributes for the tag, you use the attribute directive.

    Sample Code

    You can format output easily with tag files. You define two attributes for the tag, specifying an element color and title:

    <%@ attribute name="bgcolor" %>
    <%@ attribute name="title" %>
    
                            
  4. You create an HTML table in the tag file to build the table element with the desired background color and title in your JSP:

    Sample Code
    <table border="1" bgcolor="${color}">
     <tr>
      <td><b>${title}</b></td>
     </tr>
     <tr>
      <td bgcolor="${bgcolor}">
       <jsp:doBody/>
      </td>
     </tr>
    </table>
    
                            

Example

The full source code of the myTag.tag tag file is as follows:

Sample Code
lt;%@ tag body-content="scriptless" %>
<%@ attribute name="bgcolor" %>
<%@ attribute name="title" %>

<table border="1">   
  <tr>
    <td><b>${title}</b></td>
  </tr>
  <tr>
    <td bgcolor="${bgcolor}">
        <jsp:doBody/>     
    </td>
  </tr>
</table>

            

The full source code of the example.jsp is as follows:

Sample Code
<my:myTag bgcolor="#FF0000" title="myTable">
Some data
</my:myTag>