Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 827
Kyle Avatar asked Mar 23 '10 17:03

Kyle


People also ask

How do you use custom tags?

Custom HTML tags work in all browsers without requiring javascript registration. You can start using them immediately whenever they make sense. By default, all custom elements will display inline like a <span> . Add a display: block; property in CSS to convert them to block-level elements like a <div> .

Which is used to import a custom tag into the current JSP page?

The javax. servlet. jsp. tagext package contains classes and interfaces for JSP custom tag API.

What is custom tag in JSP What are the components that make up a tag library in JSP?

In order to use custom JSP tags, you need to define three separate components: the tag handler class that defines the tag's behavior, the tag library descriptor file that maps the XML element names to the tag implementations, and the JSP file that uses the tag library.


2 Answers

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.

like image 150
brabster Avatar answered Oct 24 '22 09:10

brabster


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>
like image 33
Alireza Fattahi Avatar answered Oct 24 '22 07:10

Alireza Fattahi