JSP custom tag, passing a non-string attribute

E

Elenturil

Hello all,
I am trying to create a custom tag to create an html table the way
I
want it to. I need to pass the custom tag an attribute or variable or
something such that the Result that it gets passed can be used in the
creation of a table, aka foreach loops. Right now I can only get an
attribute to be a string, which cannot be used in a foreach loop. I
have
seen some older posts talking about tlds, which I have not seen and
assume are reffering to older versions of jsp/jstl. Here is my code so
far.
The JSP file calls a bean that runs a query and returns a Result (set
of
rows which foreach loops can access by row and column), it then
passes the custom tag that Result hoping to get a table back.

<!--------The JSP file--------------------->
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<html>
<head>
<title>
Table
</title>
</head>
<body>
<jsp:useBean id="test" class="package.test"/>
<c:set var="thing" value="${test.testTableTag}"/>

<tags:table results="${thing}"/>
</body>
</html>

<!---The function being called by test.testTableTag------>
public Result getTestTableTag() throws Exception
{
ConnectionWrapper conn = null;
QueryResults qrs = null;
Result testTag = null;
try
{
conn = this.getDatabaseConnection();
qrs = conn.select("my query");
testTag = ResultSupport.toResult(qrs.getResultSet());
}
finally
{
qrs.close();
conn.release();
}
return testTag;

}

<!------This is the tag file--table.tag--------->
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ attribute name="results" %>

<c:set var="colorBool" value="0"/>
<table border="1">
<c:forEach items="${results.columnNames}" var="columnName">
<th>
<c:eek:ut value="${columnName}"/>
</th>
</c:forEach>
<c:forEach items="${results.rows}" var="row">
<c:choose>
<c:when test="${colorBool == '1'}">
<tr>
<c:forEach items="${row}" var="col">
<td>
<c:eek:ut value="${col.value}"/>
</td>
</c:forEach>
</tr>
<c:set var="colorBool" value="0"/>
</c:when>
<c:eek:therwise>
<tr bgcolor="#DDDDDD">
<c:forEach items="${row}" var="col">
<td>
<c:eek:ut value="${col.value}"/>
</td>
</c:forEach>
</tr>
<c:set var="colorBool" value="1"/>
</c:eek:therwise>
</c:choose>
</c:forEach>
</table>

The code works great if I call the function to set the variable from
inside the tag or if I put the code from the tag right into the jsp.
But I can't seem to pass the Result gotten in the jsp page to the tag.
Any help would be appreciated. Examples are best :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top