Java Reference
In-Depth Information
Defining a Custom Tag
DecorBox.java implements a simple custom tag. The doStartTag() method is
called to output HTML text when the <decor:box> tag is encountered, and the
doEndTag() method is called to output HTML when the closing </decor:box> tag
is encountered. These methods use nested HTML tables to produce the desired
box effect. Note that the class defines setter methods for a number of String prop-
erties. These properties define the supported attributes for the tag. For example, if
a <decor:box> tag includes a margin="10" attribute, the setMargin() method is
called with the string “10” before doStartTag() is called.
Example 18−9: DecorBox.java
package com.davidflanagan.examples.servlet;
import javax.servlet.jsp.*;
// JSP classes
import javax.servlet.jsp.tagext.*;
// Tag Library classes
import java.io.IOException;
/**
* This Tag implementation is part of the "decor" tag library. It uses HTML
* tables to display a decorative box (with an optional title) around its
* content. The various properties correspond to the attributes supported by
* the tag.
**/
public class DecorBox extends TagSupport {
// These fields contain values that control the appearance of the box
String align;
// Alignment of the box
String title;
// Title for the box
String titleColor;
// Title foreground color
String titleAlign;
// Title alignment relative to box
String color;
// Box background color
String borderColor;
// Border (and title background) color
String margin;
// Pixels between box edge and content
String borderWidth;
// Pixel width of the box border
// The following property setter methods set the values of the fields above
// When a JSP page uses this tag any tag attribute settings will be
// translated into calls to these methods.
public void setAlign(String value) { align = value; }
public void setTitle(String value) { title = value; }
public void setTitleColor(String value) { titleColor = value; }
public void setTitleAlign(String value) { titleAlign = value; }
public void setColor(String value) { this.color = value; }
public void setBorderColor(String value) { borderColor = value; }
public void setMargin(String value) { margin = value; }
public void setBorderWidth(String value) { borderWidth = value; }
/**
* This inherited method is always the first property setter invoked
* by the JSP container. We don't care about the page context here, but
* use this method to set the default values of the various properties.
* They are initialized here in case the JSP container wants to reuse
* this Tag object on multiple pages.
**/
public void setPageContext(PageContext context) {
// Important! Let the superclass save the page context object.
// We'll need it in doStartTag() below.
super.setPageContext(context);
Search WWH ::




Custom Search