tags:

views:

341

answers:

3

Hey,

Let's take a look at this bean structure for example:

public class Abean {
   private Bbean b;
}

public class Bbean {
   private ArrayList<Cbean> c;
}

public class Cbean {
   private ArrayList<Dbean> d;
}

public class Dbean {
    ....
}

So basically Abean containts everything. Now I want to make JSPs for all of these beans, where for example, the user can tell how many CBean he/she wants in BBean. So my problem is that I want to show a form for all the "child" instances automatically, for example: on d.jsp I want to show a form for every Dbean inside the Cbeans.

I've tried to embed h:dataTable-s, didn't have any success. Any help or thought will be appreciated. I hope my explanation was clear.

Thanks in advance, Daniel

A: 

I'm not sure this answers your problem, but for example in your CBean, if you have the getter for the list "d", you can use JSTL to iterate through the DBean in your CBean.

<c:forEach items="#{myCBean.d}" var="myDBean">
    <h:form>
        <!-- example form content -->
        <h:outputText value="#{myDBean.someText}"/>
        <h:inputText value="#{myDBean.exampleInput}"/>
        <h:commandButton value="#{myDBean.anAction}"/>
    </h:form>
</c:forEach>
ckarmann
I do not think this is going to work. The JSTL tags will not honour the JSF lifecycle. Even if it draws correctly, it will result in duplicate ids/input names on render and strange results during the apply request values phase on form submit.
McDowell
Indeed. It does seem to work in a facelets environment, but here in JSP it will cause problems. Thank you for pointing this out.
ckarmann
+1  A: 

I assume that since you're using JSP that you're not using Facelets?

If you were, then you could take advantage of the and manually build up a table with nested tables.

eg.

<table> 
<ui:repeat value="#{myCBEan.d}" var="myDBean">
   <tr>
      <td><h:outputText value="#{myDBean.someText}"/></td>
   </tr>
</ui:repeat>
</table>

Alternately, Richfaces has a a4j:repeat that does the same thing and can undoubtedly be used with JSPs. Also Richfaces has a rich:subTable that can be used to nest tables.

Damo
Thanks, i will try this out.
wheelie
Since it came out that I will need that solution in my project later, i'm tagging this as the right answer. Thanks for the other answers too!
wheelie
+1  A: 

Nesting dataTables is generally not a good idea. With data structures this deep you may end up with an O(n^4) iteration over the child controls, which may have consequences for performance. The standard dataTable control is quite primitive. A better approach would be to use some form of master/detail design or write a custom tree control. Since writing a custom control requires a detailed understanding of the JSF architecture, you might first want to look at 3rd party JSF libraries to see if you can find one that suits your needs.

McDowell