JSP.B.3 Processing Tag Libraries


We describe some details of how to compile tag libraries and show a sketch of some code implementing a JSP page.

JSP.B.3.1 Processing a Tag Library Descriptor

The tag library descriptor is read and information is extracted from it. Some of the actions to be performed include:

  • Record the mapping from tag to tag handler class.

  • Record the tag as a known to the JSP container so a taglib directive will introduce new actions.

  • Record information on what tags must have an empty body, to be checked on individual pages later on.

  • Record what the valid attributes are and which ones can have request-time values.

  • Record the TagExtraInfo classes, if any, associated with given tags.

  • Can be used to perform reflection on the tag handler classes to determine if the class implements Tag or BodyTag .

  • Can be used to perform introspection on the tag handler classes to determine their properties and their setter methods .

JSP.B.3.2 Processing a JSP Page

When the JSP container processes a JSP page, it will perform analysis, validation, and generation of code. Actions include:

  • Validate that actions who must have an empty body do.

  • Validate that the only attributes that appear are those indicated in the TLD.

  • Validate that the only attributes with request-time values are those indicated in the TLD.

  • If there is a TagExtraInfo class associated in the TLD, a TagData object will be created with the appropriate attribute/value entries, and will be passed to the isValid method to determine if the attributes are valid.

  • If there is a TagExtraInfo class associated in the TLD, a TagData object will be created with the appropriate attribute/value entries, and will be passed to the getVariableInfo method to determine if any scripting variables will be updated by this action at request time.

JSP.B.3.3 Generating the JSP Page Implementation Class

The JSP page implementation class generated by the JSP container includes code that:

  • Generates the appropriate setter method invocations to set values for attributes.

  • Reuses tag handlers that are not being used to reduce the number of tag handler creations.

  • Assumes that a tag handler object retains its set properties to reduce the number of method invocations.

  • Attempts to do some reorganization of setter method invocation so statically determined properties are not reset on a tag handler unnecessarily.

JSP.B.3.4 An Example

We now describe a simple example.

JSP.B.3.4.1 JSP Page Example

We will use a JSP page fragment as in Figure JSP.B-1, where " chunk " is some uninterpreted template text.

Figure JSP.B-1 A JSP Page Fragment
 chunk1  <x:foo id="myFoo" ...>                   chunk2                   <x:bar id="myBar" ...>                                    chunk3                   </x:bar>                   chunk4  </x:foo>  chunk5  <x:baz ref="myFoo" .../> 

For the example, we will assume the TLD and TagExtraInfo provide the information in Table JSP.B-1.

Table JSP.B-1. TagInfo for the Example

Tag

Handler

Variable Info ( name , type, scope)

foo

FooTag

myFoo, FooResult, AT_END

bar

BarTag

myBar, BarResult, AT_END

baz

BazTag

none

JSP.B.3.4.2 Implementation Code Fragment

The following code fragment can be used to implement the page fragment of Figure JSP.B-1.

 static {   JspFactory _fact = JspFactory.getDefaultFactory();  }  _jspService(HttpServetRequest req, HttpServletResponse res) {            PageContext pc = _fact.getPageContext(...); // once             Object tempObject = null;             int tempReturn;             // just as an example, let's initialize             // all the Tag handlers             FooTag footag = new FooTag();             BarTag bartag = new BarTag()             BazTag baztag = new BazTag();             JspWriter out = pageContext.getOut(); // initial out             // -- ditto for all other implicit objects             EVAL chunk1;   _______________________________________________                                                   Evaluate <x:foo>...</x:foo>                   _______________________________________________            EVAL chunk5;             baztag.setPageContext(pc);             baztag.setParent(null);             baztag.setRef("myFoo");             try {                (void)baztag.doStartTag();                 tempReturn = baztag.doEndTag();             } finally {                baztag.release();             }             if (tempReturn == SKIP_PAGE) {                goto endOfPage; // pseudo-code             };  endOfPage:  } 

where the evaluation of <foo>...</foo> is:

 footag.setPageContext(pc);  footag.setParent(null);  footag.setId("myFoo");  try {            if (footag.doStartTag() == EVAL_BODY_TAG) {                try {                    out = pc.pushBody();                     foobag.setBodyContent(out);                     footag.doInitBody();                 repeat2:                                             EVAL chunk2;               _______________________________________________                                                                             Evaluate <x:foo>...</x:foo>                             _______________________________________________                    EVAL chunk4;                     if (footag.doAfterBody() == EVAL_BODY_TAG) {                        goto repeat2; // pseudo-code                     }                 } finally {                    out = pc.popBody();                 }             }             tempResult = footag.doEndTag();             tempObject = pc.getAttribute("myFoo");  } finally {            footag.release();  }  FooResult myFoo = (FooResult) tempObject;  if (tempResult == SKIP_PAGE) {            goto endOfPage; // pseudo-code  } 

and the evaluation of <bar>...</bar> is essentially the same:

 bartag.setPageContext(pc);  bartag.setParent(footag);  bartag.setId("myBar");  try {            if (bartag.doStartTag() == EVAL_BODY_TAG) {                try {                    out = pc.pushBody();                     bartag.setBodyContent(out);                     bartag.doInitBody();                 repeat3:                     EVAL chunk3;                     if (bartag.doAfterBody() == EVAL_BODY_TAG) {                        goto repeat3; // pseudo-code                     }                 } finally {                    out = pc.popBody();                 }             }             tempResult = bartag.doEndTag();             tempObject = pc.getAttribute("myBar");  } finally {            bartag.release();  }  BarResult myBar = (BarResult) tempObject;  if (tempResult == SKIP_PAGE) {            goto endOfPage; // pseudo-code  } 


Java 2 Platform, Enterprise Edition. Platform and Component Specifications
Java 2 Platform, Enterprise Edition: Platform and Component Specifications
ISBN: 0201704560
EAN: 2147483647
Year: 2000
Pages: 399

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