JSP模板应用指南(下)

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

   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 should 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属性(

              "template-stack",

              PageContext.REQUEST_SCOPE);

     // if the stack's not present, create a new one和

     // put it into request scope

     if(s == null) {

       s = new Stack();

       pageContext.set属性("template-stack", s,

              PageContext.REQUEST_SCOPE);

     }

     return s;

   }

  }

  例 3.b 列出了 Put标签类和标签handler:

  例 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(): " +