2014-09-03 14 views
0

需要一些幫助。我無法做出任何讓自定義taglib正文工作的簡單示例。當沒有getBodyContent函數時,我得到一個nullpointerexception。在下面的這個例子中,我在getString行上得到了一個nullpointerexception。無法在jsp中獲得自定義taglib正文

有誰知道發生了什麼?使用Apache Tomcat 6.0.41。謝謝。

public class EscapeHtml extends BodyTagSupport { 
public int doAfterBody() { 
    BodyContent body = getBodyContent(); 
    String filteredBody = body.getString(); 
    try { 
    JspWriter out = body.getEnclosingWriter(); 
    out.print(filteredBody); 
    } 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); 
} 
} 

回答

0
BodyContent body=getBodyContent(); 

此行之後,你應該檢查一下身體具有空或不是。 null.methodname()總是返回nullpointerexception.so你要檢查的對象是空或不使用them.like低於

之前,如果(身體!= NULL)

字符串filteredBody = body.getString() ;

即使正文內容爲空或不空白,上述代碼仍可正常工作。

+0

這是正確的嘗試,謝謝。我現在正在檢查null主體,但爲什麼我的標籤主體爲空? 測試不應該是「測試」? – 2014-09-04 12:58:14

0

使用

doStartTag{ 
    return EVAL_BODY_BUFFERED; 
} 
相關問題