What Custom Tags Are and Are Not

   

Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Chapter 24.  Custom Tag Libraries


In Chapter 23, "Component-Based JSP Pages," you were introduced to the concept of JSP actions. Actions are special tags that tell the JSP page to do something, for instance, like use a JavaBean, include an external file, or set the value of a property. These actions greatly increase the usefulness of JSP pages. All the tags that we have studied thus far are built into the JSP engine.

Custom tags are similar to the action tags that you have already encountered, except that you define them yourself. There are subtle differences between the capabilities of custom tags and JavaBeans, but the primary difference is syntactic. For many JSP developers, the XML syntax of the action tags is more intuitive than the syntax for using JavaBeans, especially after you get beyond simple get() and set() methods.

Hard-core Java programmers usually prefer the JavaBean/scriptlet syntax. Web page developers often prefer the XML-style actions because its syntax is more similar to HTML than scriptlet code.

The similarities between JavaBeans and custom tags are summarized here:

  • JavaBeans and custom tags are both implemented as Java classes.

  • Both encapsulate complex behavior.

  • Both can be invoked from within JSPs.

In many situations, but not all, you can think of custom tags as an alternate syntax for calling methods in JavaBeans.

Custom tags are not identical to JavaBeans, but they resemble them. The following list shows some important differences:

  • Custom tags can manipulate JSP content, but JavaBeans cannot.

  • JSPs can pass parameters into the custom tags.

  • Custom tags require the existence of a Tag Library Descriptor File (.tld) that associates a tag name with a Java class.

  • The class that implements a tag must implement the javax.servlet.jsp.tagext.Tag interface directly, or through either the TagSupport or BodyTagSupport helper classes. JavaBeans do not require any class to be extended.

The primary impact of this list is felt during tag definition. In practical terms, you can fulfill almost any conceivable JSP requirement with either technical approach.


       
    Top
     



    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net