views:

346

answers:

2

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 to 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

A: 

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}" />
Bozho
Are you suggesting that node is my backing bean class reference?
Mark Lewis
no. before that you can have `<c:set var="node" value="#{backingBean.rootNode}"`
Bozho
+1  A: 
<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}
}
BalusC
are # and $ interchangeable in object references as above?
Mark Lewis
Yes. The `#` and `$` can do both a `get`, but only the `#` can do a `set` as well. So if you use for example `${managedbeanname}`, it won't create managed bean if it isn't already created yet (and thus it will potentially return null). The `#{managedbeanname}` instead will create bean if not done yet.
BalusC