簡體   English   中英

無法在JSP中獲得自定義Taglib主體

[英]Can't get custom taglib body in jsp

在這里需要一些幫助。 我無法舉一個簡單的示例來獲取自定義taglib主體。 當沒有getBodyContent函數時,我得到一個nullpointerexception。 在下面的示例中,我在getString行上獲得了nullpointerexception。

有人知道發生了什么嗎? 使用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);
 }
}
BodyContent body=getBodyContent();

在此行之后,您應該檢查正文是否為空。 null.methodname()始終返回nullpointerexception.so,因此在使用它們之前必須檢查對象是否為null。

如果(body!= null)

字符串filteredBody = body.getString();

上面的代碼即使主體內容為空也可以正常工作。

嘗試使用

doStartTag{
   return EVAL_BODY_BUFFERED;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM