Access variables between nested JSP tags
I would like to exchange information between two nested JSP tagx artifacts. To give an example:
list.jspx
<myNs:table data="${myTableData}">
<myNs:column property="firstName" label="First Name"/>
<myNs:column property="lastName" label="Last Name"/>
</myNs:table>
Now, the table.tagx is supposed to display the data columns as defined in the nested column tags. The question is how do I get access to the values of the property and label attributes of the nested column tags from the table tag. I tried jsp:directive.variable but that seems only to work to exchange information between a jsp and a tag, but not between nested tags.
Note, I would like to avoid using java backing objects for both the table and the column tags at all.
I would also like to know how I can access an attribute defined by a pa开发者_JS百科rent tag (in this example I would like to access the contents of the data attribute in table.tagx from column.tagx).
So it boils down to how can I access variables between nested JSP tags which are purely implemented through the tag definitions themselves (no Java TagHandler implementation desired)?
The idea is to share the data in request scope:
In myNs:table create a request scoped placeholder variable to hold the data (in your case you will need two of them: one for properties and another for labels):
<c:set var="properties" scope="request" /> <c:set var="labels" scope="request" />
- Then invoke your column tags via
<jsp:doBody />
so that columns could populate the placeholders. In myNs:column populate the placeholders, remember to keep them in request scope:
<c:choose> <c:when test="${empty properties}" scope="request"> <c:set var="properties" value="${property}" scope="request" /> </c:when> <c:otherwise> <c:set var="properties" value="${properties},${property}" scope="request" /> </c:otherwise> </c:choose>
Now, after you invoked
<jsp:doBody />
in your myNs:table you got the values populated, all you need is to split the string using comma as a separator and then do whatever you want:<table> <thead> <tr> <c:forTokens items="${labels}" delims="," var="label"> <th><c:out value="${label}" /></th> </c:forTokens> </thead> </table>
P.S.: The credits go to Spring Roo guys, take a look at their table.tagx and column.tagx.
精彩评论