JSP Tag Libraries

Скачать в pdf «JSP Tag Libraries»

The primary difference between a BodyTag and a simple Tag is that it has access to the content between its opening and closings markups (the tag’s body). We covered the BodyTag API and life cycle in chapter 4, but let’s recap the important details of the API here.

■    BodyTag introduces an additional method called doAfterBody() which is called on a tag handler after the JSP engine reads the tag’s body and processes it. This is the method in which the tag handler can inspect and/or change its processed body.

■    A tag handler accesses its processed body through its BodyContent object, which can be retrieved simply by calling getBodyContent() as long as the tag handler extends BodyTagSupport.

■    Calling getString() on the BodyContent object for a tag returns a String containing the processed body of the tag.

■    Calling getEnclosingWriter() on the BodyContent object for a tag returns a JspWriter which can be used to write back to the user. Note that this is different than the pageContext.getOut () method we used in simpler tags. We’ll discuss the reason for this difference later in this section.

■    doAfterBody() can return eval_body_tag to cause the JSP runtime engine to process the body again and call doAfterBody() once more (useful in tags that iterate). It can return SKIP_BODY to inform the JSP engine to proceed to the doEndTag() method.

A discussion of some details of how the runtime engine manages BodyTags will clarify what is happening when a BodyTag is executed, and will also answer the question about why we must use a different JspWriter (accessed via Body-Content .getEnclosingWriter()) to write to the user than we did with standard tags. This section is pretty technical and discusses some of the intricacies of the JSP runtime that you might happily take for granted. Knowing these details will, however, help you truly understand what happens to the tags you are building.

Скачать в pdf «JSP Tag Libraries»