Primefaces 进程绑定问题

Posted

技术标签:

【中文标题】Primefaces 进程绑定问题【英文标题】:Primefaces Process binding problems 【发布时间】:2013-08-09 14:00:29 【问题描述】:

我正在尝试创建一个具有可编辑和动态列和行的数据表。但是当我尝试处理表格时,我输入的值没有被处理(它们都为空)。

我将在下面发布我的代码,希望有人可以帮助我弄清楚发生了什么。

(我发布的代码只是整个代码的一部分,但它已经不起作用了)

我正在使用绑定属性创建表:

<p:panelGrid columns="2">
    <p:panelGrid columns="1">
        <h:outputText value="Fornecedor" styleClass="headerColumn" />
        <p:panelGrid columns="2" columnClasses="inputBotao">
            <p:selectOneMenu value="#orcamentoAction.idFornecedor"
                id="selForn" filter="true" filterMatchMode="startsWith">
                <f:selectItem itemLabel=" - " itemValue="#null" />
                <f:selectItems value="#orcamentoAction.fornecedores" var="f"
                    itemLabel="#f.nomeForn" itemValue="#f.idForn" />
                <p:ajax event="change" />
            </p:selectOneMenu>

            <p:commandButton action="#orcamentoAction.addFornecedor"
                icon="ui-icon-plus" process=":formEx:pfTable @this"
                update=":formEx:pfTable :formEx:selForn">
            </p:commandButton>
        </p:panelGrid>
    </p:panelGrid>

    <p:panelGrid columns="1">
        <h:outputText value="Produto" styleClass="headerColumn" />
        <p:panelGrid columns="2" columnClasses="inputBotao">
            <p:selectOneMenu value="#orcamentoAction.idProduto"
                id="selProd" filter="true" filterMatchMode="startsWith">
                <f:selectItem itemLabel=" - " itemValue="#null" />
                <f:selectItems value="#orcamentoAction.produtos" var="p"
                    itemLabel="#p.nomeProd" itemValue="#p.idProd" />
                <p:ajax event="change" />
            </p:selectOneMenu>

            <p:commandButton action="#orcamentoAction.addProduto"
                icon="ui-icon-plus" process=":formEx:pfTable @this"
                update=":formEx:pfTable :formEx:selProd">
            </p:commandButton>
        </p:panelGrid>
    </p:panelGrid>
</p:panelGrid>

<p:dataTable id="pfTable" binding="#orcamentoAction.dataTable" />

-

@ManagedBean
@ViewScoped
@Role(name = "Orçamento")
public class OrcamentoAction 

    ...

    private DataTable dataTable;

    private static final Object COLUMNS[][] = 
         "Produto", "produto.nomeProd", String.class, "width: 130px;" ,
         "Unidade", "produto.unidMedidaProd", String.class, "text-align: center; width: 50px;" ,
         "Quantidadae", "quantidade", Float.class, "width: 75px;"  ;

    private static List<UIColumn> columns;

    ...

    private void createDataTable() 
        dataTable = new DataTable();
        dataTable.setEditable(true);
        dataTable.setEditMode("cell");
        dataTable.setScrollable(true);
        dataTable.setScrollWidth("750");
        dataTable.setScrollHeight("150");
        dataTable.setEmptyMessage("Nenhum item do orçamento foi adicionado.");
        dataTable.setResizableColumns(true);
        dataTable.setVar("o");
        dataTable.setValue(list);
    

    private void createColumns() 
        columns = new ArrayList<UIColumn>();
        for (int i = 0; i < COLUMNS.length; i++) 
            Column c = new Column();
            c.setHeaderText((String) COLUMNS[i][0]);

            if (i != 2) 
                htmlOutputText outputText = new HtmlOutputText();
                outputText.setValueExpression("value",
                    FacesUtils.getExpressionFactory().createValueExpression(
                        FacesUtils.getElContext(),
                        "#o." + COLUMNS[i][1] + "",
                        (Class<?>) COLUMNS[i][2]));

                c.getChildren().add(outputText);
             else 
                CellEditor cellEditor = new CellEditor();

                HtmlOutputText outputText = new HtmlOutputText();
                outputText.setValueExpression("value",
                    FacesUtils.getExpressionFactory().createValueExpression(
                        FacesUtils.getElContext(),
                        "#o." + COLUMNS[i][1] + "",
                        (Class<?>) COLUMNS[i][2]));

                cellEditor.getFacets().put("output", outputText);

                InputText inputText = new InputText();
                inputText.setValueExpression("value",
                    FacesUtils.getExpressionFactory().createValueExpression(
                        FacesUtils.getElContext(),
                        "#o." + COLUMNS[i][1] + "",
                        (Class<?>) COLUMNS[i][2]));
                cellEditor.getFacets().put("input", inputText);

                c.getChildren().add(cellEditor);
            

            c.setStyle((String) COLUMNS[i][3]);
            columns.add(c);
        

        if (fornecedoresColumns != null && !fornecedoresColumns.isEmpty()) 
            for (int i = 0; i < fornecedoresColumns.size(); i++) 
                Fornecedor fornecedor = fornecedoresColumns.get(i);
                Column c = new Column();
                c.setHeaderText(fornecedor.getNomeForn());

                CellEditor cellEditor = new CellEditor();

                HtmlOutputText outputText = new HtmlOutputText();
                outputText.setValueExpression("value",
                    FacesUtils.getExpressionFactory().createValueExpression(
                        FacesUtils.getElContext(),
                        "#o.precos[" + i + "]", Float.class));

                NumberConverter numberConverter = new NumberConverter();
                numberConverter.setType("currency");
                numberConverter.setCurrencyCode("BRL");
                outputText.setConverter(numberConverter);

                cellEditor.getFacets().put("output", outputText);

                InputText inputText = new InputText();
                inputText.setValueExpression("value",
                    FacesUtils.getExpressionFactory().createValueExpression(
                        FacesUtils.getElContext(),
                            "#o.precos[" + i + "]", Float.class));

                numberConverter = new NumberConverter();
                numberConverter.setType("currency");
                numberConverter.setCurrencyCode("BRL");
                inputText.setConverter(numberConverter);

                cellEditor.getFacets().put("input", inputText);

                c.getChildren().add(cellEditor);

                columns.add(c);
            
        

        dataTable.setColumns(columns);
    

    public void addFornecedor() 
        if (idFornecedor != null) 
            Fornecedor fornecedor = fornecedorDao.load(idFornecedor);
            fornecedoresColumns.add(fornecedor);

            fornecedores.remove(fornecedor);

            for (OrcamentoProdutoVo vo : list) 
                vo.getPrecos().add(null);
            

            createColumns();
        
    

    public void addProduto() 
        if (idProduto != null) 
            Produto produto = produtoDao.load(idProduto);

            produtos.remove(produto);

            OrcamentoProdutoVo vo = new OrcamentoProdutoVo();

            vo.setProduto(produto);

            for (int i = 0; i < fornecedoresColumns.size(); i++) 
                vo.getPrecos().add(null);
            

            list.add(vo);
        
    

    ...


public class OrcamentoProdutoVo implements Serializable 

    private static final long serialVersionUID = 1L;

    private Produto produto;
    private Float quantidade;
    private List<Float> precos;

    public OrcamentoProdutoVo() 
        precos = new ArrayList<Float>();
    

    ....


【问题讨论】:

【参考方案1】:

传递给您的支持 bean 的值是 null,因为您的 p:dataTable 周围没有 h:form

【讨论】:

更新了我的答案以更准确地了解解决方案。 我有一个表格。但它在我的模板中,所以在这部分代码中是看不到的。

以上是关于Primefaces 进程绑定问题的主要内容,如果未能解决你的问题,请参考以下文章

<p:gmap 组件的绑定不起作用 - Primefaces 3.4.1

JSF/PrimeFaces ajax 更新破坏了 jQuery 事件侦听器函数绑定

禁用 jsf/primefaces 中的窗口功能

从支持 Bean 刷新 JSF/PrimeFaces 接口

什么是过程和更新以及它在primefaces中的工作方式[重复]

Primefaces,如何禁用表单验证?