Displaying a form with fields stored within a ArrayList<Object> using JSF and Primefaces

646 Views Asked by At

I've struggling with this issue from a few days now.

I need to display a form. The fields of the form are generated dynamically, they are stored within an ArrayList (by Object I mean an instance of the class Attribute, a very simple class with two String fields: label and value). In other words, I have no direct get and set methods to access them.

This is not a problem when the involved fields are "flat" fields, such as text fields, textareas, etc. The whole thing gets painful when it comes to diplaying data as selectboxes, or other dynamic fields (since the value of my fields is not updated).

I didn't find anything on the www about this, each example I found uses backingBean fields as value of the components, and it all works like a charm.

I've been coding for a while now, using JSF and primefaces, I've tried to add some JSTL, but I can't seem to get over the problem. I do not think that posting code is necessary at this time (I'm avaiable to give further information about it though), I would simply like to know if anyone had the same problem, or if I am looking at the problem from a wrong point of view. Is there a widely accepted approach to this kind of scenario?

Update (code):

Here is my (simplified) xhtml page (please notice that the object"card" in my backingBean (newBacking) is just a container of attributes:

<h:form id="COSMO-FORM-NEWCARD">     
    <p:dataTable var="curAttribute"
                 value="#{newBacking.card.attributes}"
                 rowIndexVar="curAttributeIndex">

        <f:facet name="header">New card: #{newBacking.card.classname}</f:facet>

        <p:column width="25%">
             <f:facet name="header">Attribute Name</f:facet>
             <h:outputText value="#{curAttribute.label}" />                    
        </p:column>

       <p:column>
           <f:facet name="header">Attribute Value</f:facet>

               <p:selectOneMenu id="#{curAttributeIndex}" value="#{curAttribute.value}">
                    <f:selectItems value="#{newBacking.lookupActivated()}" var="curItem" itemLabel="#{curItem.label}" itemValue="#{curItem.value}"/>
                </p:selectOneMenu>

       </p:column>
    </p:dataTable> 
    <br />
    <p:commandButton value="Create" action="#{newBacking.commitAction()}"/>
</h:form> 

And here is my CosmoAttribute class:

public class CosmoAttribute {

    private String label;
    private String value;
    // Other fields + getters and setters
}

Can some one explain why I am getting "Empty id attribute is not allowed" when I try to render the form? My p:selectOneMenu can't even see my curAttribute variable.

I cannot even bind my dyncamic field to a backing UIComponent, since I should create a Collection of UIComponents, and I cannot perform the binding with an element of a colletcion.

I'd like to keep the advantages of JSF, but since I've been struggling for many days, I'm ready to pursue different approaches.

0

There are 0 best solutions below