commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Scott Walters" <scottw...@hotmail.com>
Subject [jelly] define:invokeBody in a loop
Date Mon, 22 Aug 2005 23:09:15 GMT
Hi,

I tried to write a simple define taglib that uses the sql taglib to generate 
xml from a database.  The code I'm using to call the new tags looks like 
this...

	<sxml:setupQuery tableName="parent_tbl">
		<sxml:bindParams dataValueColumn="ID" bindColumn="ID"/>
		<parent_tbl>
		<sxml:toxml>
			<sxml:setupQuery tableName="child1_tbl">
				<sxml:bindParams dataValueColumn="ID" bindColumn="parent_id"/>
				<child1_tbl>
					<sxml:toxml/>
				</child1_tbl>
			</sxml:setupQuery>
		</sxml:toxml>
		</parent_tbl>
	</sxml:setupQuery>

In order to make this work, I coded the <sxml:toxml> tag to  iterate through 
each row in the result set and invoke it's body .  This works great for the 
first iteration of the loop but subsequent iterations don't return any 
output. I suspect this is because the first invokeBody causes the body to be 
removed from the script. Does this make sense?  Is there some other way to 
do this?  The complete taglib is included below.

Thanks,
Scott


<?xml version="1.0"?>

<j:jelly xmlns:j="jelly:core" xmlns:sql="jelly:sql"
		xmlns:jxml="jelly:xml" xmlns:define="jelly:define">


	<define:taglib uri="sqlexp">

		<define:tag name="setupQuery">
			<define:invokeBody/>
		</define:tag>

		<define:tag name="bindParams">
			<j:new var="parmMapTemp" className="java.util.HashMap"/>
			<j:set var="parmMap" value="${parmMapTemp}" scope="parent"/>

			<j:set var="dataValueColumnName" value="${dataValueColumn}" 
scope="parent"/>
			<j:set var="bindColumnName" value="${bindColumn}" scope="parent"/>

			<define:invokeBody/>
		</define:tag>

		<define:tag name="bindValue">
			<define:invokeBody/>
		</define:tag>

		<define:tag name="toxml">
			<j:set var="paramVal" 
value="${paramValues.peek().get(dataValueColumnName)}"/>

			<sql:query var="results" trim="false">
				select * from <j:expr value="${tableName}"/> where <j:expr 
value="${bindColumnName}"/>  = ?
				<sql:param value="${paramVal}"/>
	  		</sql:query>

			<j:invoke on="${columnNamesStack}" method="push">
				<j:arg type="java.lang.Object" value="${results}"/>
			</j:invoke>


			<j:forEach items="${results.rowsByIndex}" var="row">
				<row>
					<j:new var="columnMap" className="java.util.HashMap"/>
					<j:forEach var="columnName" 
items="${columnNamesStack.peek().columnNames}" indexVar="i">
						<jxml:element name="${columnName}">
							<j:expr value="${row[i]}"/>
						</jxml:element>
						<j:invoke on="${columnMap}" method="put">
							<j:arg type="java.lang.Object" value="${columnName}"/>
							<j:arg type="java.lang.Object" value="${row[i]}"/>
						</j:invoke>
					</j:forEach>

					<j:invoke on="${paramValues}" method="push">
						<j:arg type="java.lang.Object" value="${columnMap}"/>
					</j:invoke>

					<define:invokeBody/>

					<j:invoke on="${paramValues}" method="pop"/>
				</row>
			</j:forEach>
			<j:invoke on="${columnNamesStack}" method="pop"/>
		</define:tag>

	</define:taglib>


</j:jelly>

_________________________________________________________________
On the road to retirement? Check out MSN Life Events for advice on how to 
get there! http://lifeevents.msn.com/category.aspx?cid=Retirement


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message