繁体   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