2010-03-31 94 views
0

我有海關標籤如下。重複和標題標籤有他們的doAfterBody方法implemented.Both擴展BodyTagSupport類自定義標籤實現問題

 <csajsp:repeat reps="5"> 
       <LI> 
        <csajsp:heading bgColor="BLACK"> 
         White on Black Heading 
        </csajsp:heading> 
       </LI> 
      </csajsp:repeat> 

重複標籤類

public void setReps(String repeats) { 
       System.out.println("TESTING"+repeats); 
         //sets the reps variable. 
    } 
    public int doAfterBody() { 
       System.out.println("Inside repeate tag"+reps); 
      if (reps-- >= 1) { 
       BodyContent body = getBodyContent(); 
       try { 
       JspWriter out = body.getEnclosingWriter(); 
       System.out.println("BODY"+body.getString()); 
       out.println(body.getString()); 
       body.clearBody(); // Clear for next evaluation 
       } catch(IOException ioe) { 
       System.out.println("Error in RepeatTag: " + ioe); 
       } 
       return(EVAL_BODY_TAG); 
      } else { 
       return(SKIP_BODY); 
      } 
      } 

類標題標籤

 public int doAfterBody() 
      { 
       System.out.println("inside heading tag"); 
       BodyContent body = getBodyContent(); 
       System.out.println(body.getString()); 
       try { 
        JspWriter out = body.getEnclosingWriter(); 
        out.print("NEW TEXT"); 
       } catch(IOException ioe) { 
        System.out.println("Error in FilterTag: " + ioe); 
       } 
       // SKIP_BODY means I'm done. If I wanted to evaluate 
       // and handle the body again, I'd return EVAL_BODY_TAG. 
       return(SKIP_BODY); 
      } 
      public int doEndTag() { 
       try { 
        JspWriter out = pageContext.getOut(); 
        out.print("NEW TEXT 2"); 
       } catch(IOException ioe) { 
        System.out.println("Error in HeadingTag: " + ioe); 
       } 
       return(EVAL_PAGE); // Continue with rest of JSP page 
       } 

自定義標籤tld文件是

<taglib> 
    <tlibversion>1.0</tlibversion> 
    <jspversion>1.1</jspversion> 
    <shortname>csajsp</shortname> 
    <uri></uri> 
    <tag> 
    <name>heading</name> 
    <tagclass>com.test.tags.HeadingTag</tagclass> 
    <bodycontent>JSP</bodycontent> 
    <attribute> 
     <name>bgColor</name> 
     <required>true</required> <!-- bgColor is required --> 
    </attribute> 
    </tag> 
    <tag> 
    <name>repeat</name> 
    <tagclass>com.test.tags.RepeatTag</tagclass> 
    <info>Repeats body the specified number of times.</info> 
    <bodycontent>JSP</bodycontent> 
    <attribute> 
     <name>reps</name> 
     <required>true</required> 
     <rtexprvalue>true</rtexprvalue> 
    </attribute> 
    </tag> 
</taglib> 

其中SOP的打印順序是csajsp的

  1. setter方法:重複被調用。
  2. 打印黑色標題上的白色。即csajsp的doAfterBody:標題標籤被調用。

我不知道爲什麼它不叫csajsp:repeat標籤doAfterBody

請幫我理解這一點。

+0

沒有足夠的信息。我們需要關聯TLD文件的一部分,並且我們需要知道您的標記正在擴展哪個類/接口 – skaffman 2010-03-31 08:59:23

+0

嗨,我已經用足夠的信息更新了線程。你可以幫我嗎? – Appps 2010-03-31 12:36:12

回答

0

你的標籤延伸到什麼級別?

TagSupport的默認行爲是返回SKIP_BODY,這將跳過處理標籤正文。

BodyTagSupport的默認行爲是返回EVAL_BODY_BUFFERED,這將處理標記正文。

如果您自己實施BodyTag,那麼您需要確保正確覆蓋doStartTag以指示應評估JSP正文。