Custom JSP tag - How do I get the body of the tag?

Kyle picture Kyle · Mar 23, 2010 · Viewed 16.3k times · Source

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?

Answer

brabster picture brabster · Mar 23, 2010

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.