Dynamically add Primfaces components

I want to add Primefaces components dynamically. I am using a solution similar to this that was discussed there earlier:

<h:form>
    <h:panelGrid columns="2">
        <p:dataGrid id="categoriesGrid" value="#{bean.categories}"
            var="categoryBean" rowIndexVar="rowIndex">
            <p:column>
                <p:selectOneMenu id="categorySelect" effect="drop"
                    value="#{categoryBean.selectedCategory}" >
                    <f:selectItems value="#{categoryBean.availableCategories}"
                        var="category" itemLabel="#{category.name}"
                        itemValue="#{category}" />
                </p:selectOneMenu>
            </p:column>
        </p:dataGrid>
        <p:commandButton actionListener="#{bean.addNewCategory}"
            value="Add category" update="categoriesGrid"/>
    </h:panelGrid>
</h:form>

But there is a problem with that. An example of the answer that I received after clicking the "Add category" button:

<?xml version='1.0' encoding='UTF-8'?>
<partial-response>
<error>
    <error-name>
        class javax.faces.component.UpdateModelException
    </error-name>
    <error-message>
        <![CDATA[/createTutorial.xhtml @85,65 value=
            "#{categoryBean.selectedCategory}":java.util.NoSuchElementException]]>
    </error-message>
</error>
</partial-response>

Thanks in advance

+5
source share
1 answer

The problem was in my bean. To get the selected item, I had to implement a user interface implementation javax.faces.Converter. In my opinion, there is a lot of work for such a simple task (this converter must have access to a data source, etc.). So I did a little trick:

public class CategoryBean{

    private list<Category> availableCategories;
    private Category selectedCategory;

    public Long getCSelectedCategory(){
        // Get selected category by it id and set selectedCategory
    }

    public void setSelectedCategory(Long selectedCategory){
        return selectedCategory.getId();
    }

    // The remaining setters and getters
}

:

<p:column>
    <p:selectOneMenu id="categorySelect" effect="drop"
        value="#{categoryBean.selectedCategory}" >
        <f:selectItems value="#{categoryBean.availableCategories}"
            var="category" itemLabel="#{category.name}"
            itemValue="#{category.id}" />
    </p:selectOneMenu>
</p:column>

, itemValue , . , , Getter.

, Primefaces - . .

?

+2

All Articles