9

I have a custom jsp tag like this:

<a:customtag>
    The body of the custom tag...
    More lines of the body...
</a:customtag>

In the custom tag, how can I get the text of what the body is?

3 Answers 3

13

It's complicated because there are two mechanisms.

If you're extending SimpleTagSupport, you get getJspBody() method. It returns a JspFragment that you can invoke(Writer writer) to have the body content written to the writer.

You should use SimpleTagSupport unless you have a specific reason to use BodyTagSupport (like legacy tag support) as it is - well - simpler.

If you are using classic tags, you extend BodyTagSupport and so get access to a getBodyContent() method. That gets you a BodyContent object that you can retrieve the body content from.

1
  • 2
    It's worth noting that a JspFragment is less powerful because it is limited - it must be scriptless, where as a BodyContent is not limited in this way. A BodyContent is in essence like an internal JSP page.
    – Yoni
    Nov 27, 2013 at 12:40
11

If you are using a custom tag with jsp 2.0 approach, you can do it as:

make-h1.tag

<%@tag description="Make me H1 " pageEncoding="UTF-8"%>   
<h1><jsp:doBody/></h1>

Use it in JSP as:

<%@ taglib prefix="t" tagdir="/WEB-INF/tags"%>
<t:make-h1>An important head line </t:make-h1>
3
  • If I try to use this approach, I have a problem when the body contains other tags - if I replace An important head line with <t:MyMessageTag key="someKey"> the <jsp:doBody />seems to replace it with lower-case <t:mymessagetag key="someKey"> and won't evaluate that jsp-tag. Any idea why? Or how to handle this kind of nested tags?
    – outofmind
    Mar 27, 2018 at 7:49
  • Nested tag should work with no problem. Ask it as new question may help ! Mar 27, 2018 at 19:05
  • My mistake was to declare body-content attribute as tagdependent instead of the default scriptless - now everything's fine.
    – outofmind
    Mar 28, 2018 at 14:00
5

To expand on Brabster's answer, I've used SimpleTagSupport.getJspBody() to write the JspFragment to an internal StringWriter for inspection and manipulation:

public class CustomTag extends SimpleTagSupport {
    @Override public void doTag() throws JspException, IOException {
        final JspWriter jspWriter = getJspContext().getOut();
        final StringWriter stringWriter = new StringWriter();
        final StringBuffer bodyContent = new StringBuffer();

        // Execute the tag's body into an internal writer
        getJspBody().invoke(stringWriter);

        // (Do stuff with stringWriter..)

        bodyContent.append("<div class='custom-div'>");
        bodyContent.append(stringWriter.getBuffer());
        bodyContent.append("</div>");

        // Output to the JSP writer
        jspWriter.write(bodyContent.toString());
    }
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.