JSP template Application Guide (II)

Source: Internet
Author: User

Execution Template
The template discussed here will be executed under three custom labels:

Template: insert

Template: Put

Template: Get

The insert tag contains a template, but before it is included, the put tag stores some information-name, Uri, and boolean values (used to specify whether the content is included or directly displayed) -- About the content contained in the template. The specified content is included (or displayed) in template: Get, and the information will be accessed later.

Template: Put stores the bean in the request region (but not directly), because if the two templates use the same content name, a nested template overwrites the content in the encapsulation template.

To ensure that each template can only access its own information, template: insert retains a hashtable stack. Each insert starts a label to create a hashtable and put it into the stack. Encapsulate put labels to create beans and save them to the recently created hashtable. Then, access the bean in hashtable by the get tag in the contained template. Figure 4 shows how the stack is retained.

Figure 4. Click to enlarge the parameters of the Request region storage template (24 KB)

In Figure 4, each template contains the footer footer.html and footer_2.html. If the bean is directly stored in the request area, step 5 in Figure 4 overwrites the footer bean specified in step 2.

Template tag execution
Next, we will analyze the execution of the three template labels: insert, put, and get. Let's start with figure 5. This chart shows the execution sequence of insert and put tag events when a template is used.

Figure 5. Click to enlarge the execution sequence of put and insert tags (24 KB)

If a template stack does not exist, the insert start tag will be created and placed in the request area. Then a hashtable is created and put into the stack.

Each put start tag creates a pageparameter bean and stores it in the hashtable created by the encapsulated insert tag.

The insert end tag contains this template. This template uses the get tag to access the bean created by the put tag. After the template is processed, the hashtable created by the label starting with insert is cleared from the stack.

Figure 6 shows the sequence chart of template: Get.

Figure 6. Click to enlarge the get tag sequence chart (11 KB)

Template tag list
The tag handler is simple. In Example 3. A, the insert label class -- tag handler is listed.

Example 3. A. inserttag. Java

Packagetags. templates;

Import java. util. hashtable;

Import java. util. Stack;

Import javax. servlet. jsp. jspexception;

Import javax. servlet. jsp. pagecontext;

Import javax. servlet. jsp. tagext. tagsupport;

Public class inserttagextendstagsupport {

Private stringtemplate;

Private stack;

// Setter method fortemplate attribute

Public void settemplate (stringtemplate ){

This. template = template;

}

Public int dostarttag () throws jspexception {

Stack = getstack (); // obtain a reference to thetemplate Stack

Stack. Push (New hashtable (); // push new hashtable onto Stack

Return eval_body_include; // pass tagbody through unchanged

}

Public int doendtag () throws jspexception {

Try {

Pagecontext. Include (Template); // includetemplate

}

Catch (exception ex) {// ioexception or servletexception

Throw new jspexception (ex. getmessage (); // recast exception

}

Stack. Pop (); // pop hashtable off Stack

Return eval_page; // evaluate the rest of the page after the tag

}

// Taghandlers shoshould always implement release () because

// Handlers can be reused by the JSP Container

Public void release (){

Template = NULL;

Stack = NULL;

}

Public stack getstack (){

// Try to get Stack from request Scope

Stack S = (stack) pagecontext. Get attribute (

"Template-stack ",

Pagecontext. request_scope );

// If the stack's not present, create a new one and

// Put it into request Scope

If (S = NULL ){

S = new stack ();

Pagecontext. Set attributes ("template-stack", S,

Pagecontext. request_scope );

}

Return S;

}

}

Example 3. B lists the put tag class and tag handler:

Example 3. B. puttag. Java

Packagetags. templates;

Import java. util. hashtable;

Import java. util. Stack;

Import javax. servlet. jsp. jspexception;

Import javax. servlet. jsp. tagext. tagsupport;

Import beans. Templates. pageparameter;

Public class puttagextendstagsupport {

Private string name, content, direct = "false ";

// Setter methods for put tag attributes

Public void setname (string s) {name = s ;}

Public void setcontent (string s) {content = s ;}

Public void setdirect (string s) {direct = s ;}

Public int dostarttag () throws jspexception {

// Obtain a reference to enclosing insert tag

Inserttagparent = (inserttag) getancestor (

"Tags. Templates. inserttag ");

// Puttags must be enclosed in an insert tag

If (parent = NULL)

Throw new jspexception ("puttag. dostarttag ():" +

"No inserttagancestor ");

// Gettemplate Stack from insert tag

Stacktemplate_stack = parent. getstack ();

// Template stack shocould never be null

If (template_stack = NULL)

Throw new jspexception ("puttag: notemplate stack ");

// Peek at hashtable on the stack

Hashtable Params = (hashtable) template_stack.peek ();

// Hashtable shocould never be null either

If (Params = NULL)

Throw new jspexception ("puttag: No hashtable ");

// Put a new pageparameter in the hashtable

Params. Put (name, new pageparameter (content, direct ));

Return skip_body; // not interested in tagbody, if present

}

// Taghandlers shoshould always implement release () because

// Handlers can be reused by the JSP Container

Public void release (){

Name = content = direct = NULL;

}

// Convenience Method for Finding ancestor names

// A specific class name

Privatetagsupport getancestor (string classname)

Throws jspexception {

Class Klass = NULL; // can't name variable "class"

Try {

Klass = Class. forname (classname );

}

Catch (classnotfoundexception ex ){

Throw new jspexception (ex. getmessage ());

}

Return (tagsupport) findancestorwithclass (this, Klass );

}

}

Puttag. dostarttag creates a pageparameter bean, which is listed in Example 3. C and then stored in the request area.

Example 3. C. pageparameter. Java

Package beans. templates;

Public class pageparameter {

Private string content, direct;

Public void setcontent (string s) {content = s ;}

Public void setdirect (string s) {direct = s ;}

Public String getcontent () {return content ;}

Public Boolean isdirect () {return Boolean. valueof (direct). booleanvalue ();}

Public pageparameter (string content, string direct ){

This. content = content;

This. Direct = direct;

}

}

Pageparameter is used as a simple placeholder. Let's take a look at the gettag class and tag handler in Example 3. D:

Example 3. d. gettag. Java

Packagetags. templates;

Import java. util. hashtable;

Import java. util. Stack;

Import javax. servlet. jsp. jspexception;

Import javax. servlet. jsp. pagecontext;

Import javax. servlet. jsp. tagext. tagsupport;

Import beans. Templates. pageparameter;

Public class gettagextendstagsupport {

Private string name;

// Setter Method for name attribute

Public void setname (string name ){

This. Name = Name;

}

Public int dostarttag () throws jspexception {

// Obtain reference totemplate Stack

Stack stack = (stack) pagecontext. Get attribute (

"Template-stack", pagecontext. request_scope );

// Stack shocould not be null

If (stack = NULL)

Throw new jspexception ("gettag. dostarttag ():" +

"No stack ");

// Peek at hashtable

Hashtable Params = (hashtable) stack. Peek ();

// Hashtable shocould not be null

If (Params = NULL)

Throw new jspexception ("gettag. dostarttag ():" +

"No hashtable ");

// Get page parameter from hashtable

Pageparameter Param = (pageparameter) Params. Get (name );

If (Param! = NULL ){

String content = Param. getcontent ();

If (Param. isdirect ()){

// Print content if direct attribute is true

Try {

Pagecontext. getout (). Print (content );

}

Catch (Java. Io. ioexception ex ){

Throw new jspexception (ex. getmessage ());

}

}

Else {

// Include content if direct attribute is false

Try {

Pagecontext. getout (). Flush ();

Pagecontext. Include (content );

}

Catch (exception ex ){

Throw new jspexception (ex. getmessage ());

}

}

}

Return skip_body; // not interested in tagbody, if present

}

// Taghandlers shoshould always implement release () because

// Handlers can be reused by the JSP Container

Public void release (){

Name = NULL;

}

}

Gettag. dostarttag returns the page parameter bean from the request region and obtains the content and direct attributes from the bean. Then, the content can be included or displayed based on the direct property value.

Conclusion
A template is a simple and useful concept. The encapsulation layout of a template can minimize the impact of layout changes. In addition, templates can differentiate different content based on different users. They can also be nested into other templates and JSP pages.

<Full text>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.