简体   繁体   中英

Can't get custom taglib body in jsp

need some help here. I can't make any simple example of getting a custom taglib body work. When there is no getBodyContent function I get a nullpointerexception. In this example below I get a nullpointerexception on the getString line.

Does anyone know what is happening? Using Apache Tomcat 6.0.41. Thanks.

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();

After this line you should check the body has null or not. null.methodname() always return nullpointerexception.so you have to check the object is null or not before using them.like below

if(body!=null)

String filteredBody=body.getString();

The above code works fine even body content is empty or not.

Try using

doStartTag{
   return EVAL_BODY_BUFFERED;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM