I changed my DBXML taglib last night so it will work with JSTL tags. I left out EL support because I didn't need it yet.
As to the kind of changes needed for a taglib to be integrated with JSTL, classes need to support the JavaBeans method naming design pattern and implement common interfaces like Iterator or Collection. For example JSTL Core forEach tag requires a variable set with an array of primitive data types or an object or its member implementing Iterator or Collection interface.
Unfortunately, DBXML's Java binding classes looks like they were written by a C programmer with little understanding common Java practices nor common performance pitfalls. XmlResults class, for example, doesn't follow the JavaBeans method naming design pattern and doesn't provide an Iterator interface. Not surprising since it doesn't even override the toString method. It also instantiates a new XmlValue for each result item.
To get around these problems, I made these changes to DBXML Java binding:
- added XmlValue.setCPtr method so XmlValue can be reused.
- added XmlValue.nextValue method with an XmlValue out parameter.
- added XmlValue.toString()
- added getXXX versions of asXXX methods.
- added XmlResults.getItems() that resets and returns an Iterator implementation.
With above changes, I was able to use my DBXML taglib with JSTL tags like this:
<%@ taglib prefix="dbxml"
uri="/WEB-INF/dbxml.tld" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<html><head><title>DBXML Test</title></head><body>
<h2>My Posts about Flash</h2>
<dbxml:setDataSource dataSource="dbxml/blog"/>
<dbxml:query var="titles"
xpath=
"/donpark/post/item/title[contains(.,'Flash')]/text()"
resultType="values" />
<c:forEach var="item" items="${titles.items}">
<h4><c:out value="${item.string}"/></h4>
</c:forEach>
</body></html>
Which returns:
<h4>Funniest Comment about Flash Mobs</h4>
<h4>Flash Mob Discount?</h4>
<h4>Flash Mobs in the Bay Area?</h4>
<h4>Flashing XML</h4>
Note that EL expression ${titles.items} invokes XmlResults.getItems method and ${item.string} invokes XmlResults.getString method according to JavaBeans design pattern. Adding EL support to a taglib involves passing attribute values and body content through the EL parser so users can use EL expressions when using your tags.