简体   繁体   English

动态添加Primefaces组件

[英]Adding Primefaces components dynamically

I want to add Primefaces components dynamically. 我想动态添加Primefaces组件。 I'm using solution similar to this one , which 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 problem with it. 但它有问题。 There is example of respond I get after "Add category" button was clicked: 在点击“添加类别”按钮后,我得到了回复示例:

<?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 提前致谢

The problem was with my bean. 问题出在我的豆子上。 In order to get selected item, I had to implement custom implementation of javax.faces.Converter interface. 为了获得所选项,我不得不实现javax.faces.Converter接口的自定义实现。 In my opinion it's quite much work to do for such simple problem (this converter must be able to have access to data source and so on). 在我看来,为这样一个简单的问题做很多工作(这个转换器必须能够访问数据源等)。 So I've made a little trick: 所以我做了一个小技巧:

public class CategoryBean{

    private list<Category> availableCategories;
    private Category selectedCategory;

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

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

    // The remaining setters and getters
}

And the corresponding piece of page code now looks like: 相应的页面代码现在看起来像:

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

Please, pay your attention to the itemValue attribute and access methods that are shown.The bug I had was wrong getter return type. 请注意itemValue属性和显示的访问方法。我遇到的错误是错误的getter返回类型。

Concluding , the only problem with adding new Primefaces components dynamically in my case was returning selected value. 最后 ,在我的情况下动态地添加新的组件Primefaces唯一的问题是返回选择的值。 You may implement converter or use similar trick. 您可以实现转换器或使用类似的技巧。

Is such trick a good solution in your opinion? 在你看来,这样的技巧是一个很好的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM