Accessing java-based DOM tree directly from JSF/richfaces
Based on this question I have a couple of other questions:
1) the map in this question which is made available to jsf is actually one of a number, so i'm now not sure what the backing bean method's return type should now be. if i modify it's current Array<String>
return type t开发者_如何学JAVAo Array<Map Integer, Map<String, String[]>>>
(or ArrayList<Map Integer, Map<String, String[]>>>
?) would it just be a case of further nesting the iterator on the jsf side? Trouble is an Array/ArrayList isn't a Map and I'm unsure how this then looks in jsf. would this be correct:
<c:forEach items="#{bean.map}" var="entry"> <!-- array -->
<c:forEach items="#{entry.value}" var="nentry"> <!-- map -->
<h:outputText value="Key: #{nentry.key}, Values:" /> <!-- integer -->
<c:forEach items="#{nentry.value}" var="nnentry"> <!-- sub map -->
<h:outputText value="Key: #{nnentry.key}, Values:" /> <!-- string -->
<c:forEach items="#{nnentry.value}" var="nnnentry"> <!-- string[] -->
<h:outputText value="#{nnnentry}" />
</c:forEach><br />
</c:forEach><br />
</c:forEach><br />
</c:forEach>
?
2) what i'm really storing in this map is xpath rips from an XML DOM tree parsed on the java side. i'm now thinking i can access this java-based DOM tree from JSF directly without having to use XPath -> ArrayOfMaps and return that. In an XML file which looks something like this, is there a better way than using the above method?:
<test>
<testid>1</testid>
<testname>myName</testname>
<inst>
<id>1</id>
<src>C:\my\path</src>
<mask>.*\.\w{3}</mask>
<mask>.*\.x</mask>
</inst>
<inst>
<id>2</id>
<src>C:\my\otherpath</src>
<mask>.*\.\w{3}</mask>
<mask>.*\.x</mask>
</inst>
</test>
Thanks again Mark
<c:forEach items="#{bean.map}" var="entry"> <!-- array --> <c:forEach items="#{entry.value}" var="nentry"> <!-- map -->
This is wrong. Each iteration over an ArrayList
doesn't return a Map.Entry
object at all as you seem to think. It just returns the individual element of the List
(which is in your case a Map
). Here's how it should look like:
<c:forEach items="#{bean.list}" var="map"> <!-- array -->
<c:forEach items="#{map}" var="entry"> <!-- map -->
In nutshell, a c:forEach
iteration over an List
or Object[]
as follows
<c:forEach items="${array}" var="item">
...
</c:forEach>
is best to be interpreted in raw Java code as
for (Object item : array) {
// ...
}
while a c:forEach
iteration over Map
as demonstrated in your previous topic is best to be interpreted in raw Java code as:
for (Entry<K, V> entry : map.entrySet()) {
K key = entry.getKey(); // ${entry.key}
V value = entry.getValue(); // ${entry.value}
}
This article show a way to use recursion with JSTL. You can give it a try:
<c:forEach var="node" items="${node.children}">
<c:set var="node" value="${node}" scope="request"/>
<jsp:include page="node.jsp"/>
</c:forEach>
Just, in order to accommodate your case, you can put the following before the loop:
<c:set var="node" value="#{backingBean.rootNode}" />
精彩评论