Reputation: 485
I would like to create a custom JSP tag that can be used like this:
<mytags:myTag>
<p>My content!</p>
</mytags:myTag>
In the tag, I would like to process the content of the body just like I would use any other attribute. So, the tag definition would look something like this - but the body would not be an attribute but something else.
mytag.tag:
<%@taglib prefix="mytags" tagdir="/WEB-INF/tags/mytags" %>
<%@attribute name="body" required="true"%>
<div>
<c:if test="${fn:contains(body, 'test')}">
<p>Found test string<p>
</c:if>
</div>
Obviously, something like <jsp:doBody/>
or <jsp:invoke fragment="body" />
will not help me. Also, it would seem a bit overly complicated to create a Java tag for this purpose.
Upvotes: 3
Views: 1099
Reputation: 273
It is possible to capture the body content using the <jsp:dobody>
action through its var
attribute as demonstrated in this article. The body content will be added as an attribute to the pageContext
of your tag file and can be accessed through an expression.
mytag.tag
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<jsp:doBody var="body"/>
<div>
<c:if test="${ fn:contains(body, 'test') }">
<p>Found test string</p>
</c:if>
</div>
Upvotes: 1