JSP模板应用指南(下)

2019-05-27 21:40:19于丽


                  "No Inserttagancestor");

     // gettemplate stack from insert tag

     Stacktemplate_stack = parent.getStack();

     //template stack should 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 should 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 should 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 with

   // 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建立了一个 PageParameter bean – 在例 3.c中列出——然后存储到请求区域。

  例 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将作为简单的占位符使用。我们来看一看例 3.d中的Gettag类和tag handler:

  例 3.d. GetTag.java

  packagetags.templates;

  import java.util.Hashtable;

  import java.util.Stack;

  import javax.servlet.jsp.JspException;

  import javax.servlet.jsp.PageContext;