Access variables between nested JSP tags

I would like to exchange information between two nested JSP tag 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, table.tagx should display the columns of data as defined in the nested column tags. The question is how to access property values ​​and label attributes of nested column tags from a table tag. I tried jsp: directive.variable, but it seems to work only for exchanging information between jsp and a tag, but not between nested tags.

Note that I would like to avoid using java support objects for tables and column tags in general.

I would also like to know how I can access the attribute defined by the parent tag (in this example, I would like to access the contents of the data attribute in table.tagx from column.tagx).

So this boils down to how I can access variables between nested JSP tags that execute exclusively through tag definitions (no Java TagHandler implementation required)?

+4
source share
1 answer

The idea is to share data in the query area:

  • In the myNs table: create a variable of the replacement object with a request to store 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 call the column tags via <jsp:doBody /> so that the columns can fill in placeholders.
  • In the column myNs: fill in the placeholders, remember to leave them in the request area:

     <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 called <jsp:doBody /> in the table myNs :, you got the filled values, all you need to do is break the string with a 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> 

PS: Credits go to Spring Roo guys, look at their table.tagx and column.tagx .

+2
source

Source: https://habr.com/ru/post/1305901/


All Articles