Folks,
Hi. I'm just learning Java, JSP & JSTL, and I've hit a bit of a curly one.
What's the best way to pass dynamic-attributes from a JSTL tag to another JSTL tag? Is there a way to pass them directly as a Map?
I wrote a generic-html-table-maker custom JSTL tag called "tabulator.tag" (based on one of the examples in JavaServer Pages by Hans Bergsten).
"tabulator.tag"
<%-- format contents as a HTML table --%>
<%@ tag body-content="empty" dynamic-attributes="dynattrs"
import="java.util.*" %>
<%@ attribute name="content" required="true" type="java.util.Map" %>
<%@ attribute name="caption" %>
<%@ attribute name="headers" type="java.lang.Boolean" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<table
<c:forEach items="${dynattrs}" var="a">
${a.key}="${a.value}"
</c:forEach>
>
<c:if test="${!empty caption}">
<caption>${caption}</caption>
</c:if>
<c:if test="${headers}">
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</c:if>
<c:forEach items="${content}" var="row">
<tr>
<td>${row.key}</td>
<td>${row.value}</td>
</tr>
</c:forEach>
</table>
This works fine when called from directly from jsp as below:
"tabulator_test.jsp"
<%-- format request headers as a HTML table --%>
<%@ page contentType="text/html" %>
<%@ taglib prefix="my" tagdir="/WEB-INF/tags/mytags" %>
<my:tabulator
headers="true" caption="Tabulated Request Headers" content="${header}"
border="1" cellspacing="0" cellpadding="2"
/>
BUT... Now I'm trying to write a "requestHeaders.tab" just to "centralise" the table attributes... but I can't figure out how to pass the dynamic-attributes (containing the html table attributes) from "requestHeaders.tab" through to tabulator.tab
"headers2.tab" (aka "requestHeaders.tab")
<%-- format request headers as a HTML table --%>
<%@ tag body-content="empty" dynamic-attributes="dynattrs" %>
<%@ attribute name="caption" required="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="my" tagdir="/WEB-INF/tags/mytags" %>
<%-- build a string of the dynamic-attributes --%>
<c:set var="s">
<c:forEach items="${dynattrs}" var="a">
${a.key}="${xmlEncode(a.value)}"
</c:forEach>
</c:set>
s: ${s}
<%-- and pass them to tabulator --%>
<my:tabulator content="${header}" caption="${caption}" ${s} />
... throws the below
Tomcat Error when called by ...
"headers2.jsp"
<%@ page contentType="text/html" %>
<%@ taglib prefix="my" tagdir="/WEB-INF/tags/mytags" %>
<html>
<body>
<my:headers2 caption="Request Headers"
border="1" cellspacing="0" cellpadding="5"
/>
</body>
</html>
Apache Tomcat/5.5.17 - Error report
exception
org.apache.jasper.JasperException: /WEB-INF/tags/mytags/headers2.tag(14,55) Unterminated <my:tabulator tag
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
org.apache.jasper.JasperException: /WEB-INF/tags/mytags/headers2.tag(14,55) Unterminated <my:tabulator tag
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)
....
This is just an intellectual exersice, but it's sucked me in... any help be greatly appreciated.
Thanx. KRC>