BodyTag interface of Tag API

In this post, you will learn:

  • What is the role of BodyTag interface in Tag API?
  • What is the role of BodyContent class of Tag API?
  • How to implement BodyTag with the help of BodyTagSupport?

BodyTag interface:

BodyTag is a sub interface of IterationTag. It adds following method to the tag life cycle:

setBodyContent: This method facilitate processing of the tag body by the Tag itself. It has following syntax:

public void setBodyContent();

In addition to this method, the BodyTag interface defines BodyTag.EVAL_BODY_BUFFERED static final int data member to be used as a return value of the doStartTag() method. When this value is returned by doStartTag() method only then the setBodyContent() method is invoked by the Tag processor.

Following diagram describes the complete tag life cycle:

taglifecycle_complete

Description of the diagram:

1. When a custom tag is encountered, Tag Processor creates or obtains a Tag object, initializes it and invokes doStartTag() method.

2. This method generates contents which replaces the opening tag in the response and decides whether the body is to be skipped, or processed by the Tag processor or the Tag.

3. When SKIP_BODY is returned by the doStartTag(), the body is skipped and doEndTag() method is invoked by the Tag processor.

4. When EVAL_BODY_INCLUDE is returned by the doStartTag(), the body is processed by the Tag processor and doAfterBody() method is invoked. When SKIP_BODY is returned by the doAfterBody(), the doEndTag() method is invoked.

5. When EVAL_BODY_BUFFERED is returned by the doStartTag(), Tag processor stores the body in a BodyContent object and provides this object to the Tag as parameter of setBodyContent() method so that the Tag can process the body on its own. After this the doEndTag() method is invoked.

6. The doEndTag() method generates contents which replaces the closing tag in the response and decides whether rest of the page is to be skipped, or processed.

The BodyContent class:

BodyContent is a helper class of the Tag API. An object of this class is used by the Tag processor to provide the body to the Tag object when the body is to be processed by the Tag itself. Commonly used methods of this class are:

getString(): This method is used by a Tag object to obtain contents of the body as a String. It has following syntax:

public String getString();

getInputStream(): This method is used by a Tag object to obtain an InputStream for reading the body contents. It has following syntax:

public InputStream getInputStream();

The BodyTagSupport class:

BodyTagSupport is a helper class of the Tag API. It extends TagSupport class and implements BodyTag interface. It is used as a super class for those custom tag classes which process body contents on their own. It has following implementation:

package javax.servlet.jsp.tagext;

public class BodyTagSupport extends TagSupport implements BodyTag {

	protected BodyContent bodyContent;
	
	public void setBodyCotent (BodyContent content)
	{
		bodyContent=content;
	}
	
	public BodyContent getBodyContent()
	{
		return bodyContent;
	}
       //doStartTag() is overridden so that body can be processed by the tag itself.
	public int doStartTag()
        {
           return EVAL_BODY_BUFFERED;
        }
	//other utility methods
	
}


Its time to recap the main concepts:

  1. A custom tag can do following things with its body: the body can be skipped, processed by the Tag processor, or processed by the Tag itself.
  2. If a Tag processes its body then it need to return EVAL_BODY_BUFFERED from the doStartTag method and need to define setBodyContent() method.
  3. This return value and setter method are provided by BodyTag() interface.
  4. BodyTagSupport class implements this interface.
  5. An object of type BodyContent is used by the Tag processor to provide the body contents to the Tag object.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *