In there an example on how to create a JSP page that uses other JSP pages as components? How does this work with tag libraries that contain if/then/else/loop tags whose bodies must include component JSP pages?

Stacy Curl

Normally when you call one JSP page from another you have no significant access to the result of the called JSP. So if I have


Hi, I like nice clean JSP output
<jsp:include page="gobbledegook.jsp" flush="true"/>
Doh!
When it would be much nicer to be able to do this:

<%@ taglib uri="blahblah" prefix="myTags"%>
Hi, I like nice clean JSP output
<myTags:cleanUp>
<jsp:include page="gobbledegook.jsp" flush="true"/>
</myTags:cleanUp>
Wahoo!
This can be accomplish with the tag class:
public class TakeOutTheTrashTag extends BodyTagSupport
{
  public int doStartTag() throws JspTagException
  {
    return EVAL_BODY_TAG;
  }

  public int doAfterBody() throws JspTagException
  {
    try {
      if (bodyContent != null) {
        String messy = bodyContent.getString();
        String clean = takeOutTheTrash(messy);
        bodyContent.getEnclosingWriter().print(clean);
      }
    } catch (IOException ie) {}

    return EVAL_PAGE;
  }

  public int doEndTag() throws JspTagException
  {
    return EVAL_PAGE;
  }

  private String takeOutTheTrash(String messy)
  {
    String cleanedUp = messy;
    //...
    return cleanedUp;
  }
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact