Primefaces处理绑定问题

时间:2013-08-08 00:46:51

标签: jsf-2 binding primefaces

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

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

(我发布的代码只是整个代码的一部分,但它已经无效了)

我使用绑定属性创建表:

<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 个答案:

答案 0 :(得分:0)

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