对话框内的数据表不会向 Bean 发送数据



我正在尝试在对话框中制作一个选择数据表。但这没有奏效。我花了几个小时寻找解决方案,但没有找到任何地方。

我只想做一个表,当用户选择一行并单击 Selecionar 按钮时,对象项目被发送到主 bean。但它不调用 set 方法。我已经尝试了很多方法来解决这个问题。他们都没有工作。

这是我的 xhtml:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"                         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">
    <h:head>
    </h:head>
    <h:body>
        <p:dialog widgetVar="selecionar_produto_WV" modal="true"
        showEffect="clip">
            <form id="form_selecionar_produto">
                <p:dataTable var="produto" paginator="true" rows="5"
                    emptyMessage="Não existem produtos cadastrados!"
                    selectionMode="single" rowKey="#{produto.id}"
                    selection="#{selecionarProdutoMBean.produto}"
                    value="#{selecionarProdutoMBean.produtos}" id="table"
                    style="width:900px">
                    <p:column styleClass="l" headerText="Nome" sortBy="#{planta.nome}">
                        <p:outputLabel value="#{produto.nome}" />
                    </p:column>
                    <p:column styleClass="l" headerText="Descrição"
                sortBy="#{produto.descricao}">
                        <p:outputLabel value="#{produto.descricao}" />
                    </p:column>
                    <p:column styleClass="l" headerText="Valor"
                        sortBy="#{produto.valor}">
                        <p:outputLabel value="R$#{produto.valor}" />
                    </p:column>
                    <p:column styleClass="l" headerText="Produtor"
                        sortBy="#{produto.produtor.nome}">
                        <p:outputLabel value="#{produto.produtor.nome}" />
                    </p:column>
                    <p:column styleClass="l" headerText="Entrada"
                        sortBy="#{produto.entrada}">
                        <p:outputLabel value="#{produto.entrada}">
                            <f:convertDateTime pattern="dd/MM/yyyy" />
                        </p:outputLabel>
                    </p:column>
                    <f:facet name="footer">
                        <p:commandButton process="table" icon="ui-icon-circle-check"
                    value="Selecionar" oncomplete="PF('selecionar_produto_WV').hide();" />
                    </f:facet>
                </p:dataTable>
            </form>
        </p:dialog>
    </h:body>
 </html>

这是我的豆子:

@ManagedBean(name = "selecionarProdutoMBean")
@SessionScoped
public class SelecionarProdutoMBean {
private Produto produto;
private List<Produto> produtos;
@EJB
private IManterProdutoSBean<Produto> bean = new ManterProdutoSBean();
public SelecionarProdutoMBean() {
    produtos = new ArrayList<>();
    produto = new Produto();
}
public void onRowSelect(SelectEvent event) {
    Produto p = (Produto) event.getObject();
    System.out.println(p.getNome());
}
public List<Produto> getProdutos() {
    produtos = bean.listar();
    return produtos;
}
public void setProdutos(List<Produto> produtos) {
    this.produtos = produtos;
}
public Produto getProduto() {
    return produto;
}
public void atualizar() {
    System.out.println("789123");
}
public void setProduto(Produto produto) {
    this.produto = produto;
}

}

下面是调用包含表的对话框的对话框

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:p="http://primefaces.org/ui">
    <h:head>
    </h:head>
    <h:body>
        <p:dialog widgetVar="iu01_13_01" modal="true" showEffect="clip">
            <p:ajax event="close" update="iu01_13_form"
            listener="#{iu01_13MBean.limpar()}" />
            <div id="template">
                <h:form id="iu01_13_01_form">
                    <p:panelGrid columns="2">
                        <p:outputLabel value="Nome:" />
                        <p:inputText id="Nome" value="#{iu01_13MBean.venda.nome}" />
                        <p:outputLabel value="Data de venda:" />
                        <p:calendar id="Registro" value="#{iu01_13MBean.venda.data}" />
                        <p:outputLabel value="Valor:" />
                        <p:inputText id="Valor" value="#{iu01_13MBean.venda.valor}" />
                        <p:outputLabel value="Detalhes:" />
                        <p:inputText id="Detalhes" value="#{iu01_13MBean.venda.detalhes}" />
                        <p:outputLabel value="Produto:" />
                        <p:panelGrid columns="2">
                            <p:commandButton actionListener="#{iu01_08MBean.updateList()}"
                                value="Selecionar produto" onclick="PF('selecionar_produto_WV').show();" />
                            <p:inputText id="Produto" value="#{iu01_08MBean.produto.nome}" />
                        </p:panelGrid>
                        <p:commandButton value="FINALIZAR" action="#{iu01_13MBean.salvar}"
                    update="iu01_13_01_form, iu01_13_form"
                            onclick="PF('iu01_13_01').close();" />
                        <p:outputLabel />
                    </p:panelGrid>
                </h:form>
            </div>
        </p:dialog>
        <ui:include src="selecionar_produto.xhtml" />
    </h:body>
 </html>

试试这个:

你的 XHTML 应该是这样的:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"                         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:p="http://primefaces.org/ui">
        <h:head>
        </h:head>
        <h:body>
 <h:form id="form_selecionar_produto">
            <p:dialog widgetVar="selecionar_produto_WV" modal="true"
            showEffect="clip">

                    <p:dataTable var="produto" paginator="true" rows="5"
                        emptyMessage="Não existem produtos cadastrados!"
                        selectionMode="single" rowKey="#{produto.id}"
                        selection="#{selecionarProdutoMBean.produto}"
                        value="#{selecionarProdutoMBean.produtos}" id="table"
                        style="width:900px">
                        <p:column styleClass="l" headerText="Nome" sortBy="#{planta.nome}">
                            <p:outputLabel value="#{produto.nome}" />
                        </p:column>
                        <p:column styleClass="l" headerText="Descrição"
                    sortBy="#{produto.descricao}">
                            <p:outputLabel value="#{produto.descricao}" />
                        </p:column>
                        <p:column styleClass="l" headerText="Valor"
                            sortBy="#{produto.valor}">
                            <p:outputLabel value="R$#{produto.valor}" />
                        </p:column>
                        <p:column styleClass="l" headerText="Produtor"
                            sortBy="#{produto.produtor.nome}">
                            <p:outputLabel value="#{produto.produtor.nome}" />
                        </p:column>
                        <p:column styleClass="l" headerText="Entrada"
                            sortBy="#{produto.entrada}">
                            <p:outputLabel value="#{produto.entrada}">
                                <f:convertDateTime pattern="dd/MM/yyyy" />
                            </p:outputLabel>
                        </p:column>
                        <f:facet name="footer">
                            <p:commandButton process="table" icon="ui-icon-circle-check"
                        value="Selecionar" oncomplete="PF('selecionar_produto_WV').hide();" />
                        </f:facet>
                    </p:dataTable>
            </p:dialog>
</h:form>
        </h:body>
 </html>

调用包含表的对话框的对话框应如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:p="http://primefaces.org/ui">
    <h:head>
    </h:head>
    <h:body>
   <h:form id="iu01_13_01_form">
        <p:dialog widgetVar="iu01_13_01" modal="true" showEffect="clip">
            <p:ajax event="close" update="iu01_13_form"
            listener="#{iu01_13MBean.limpar}" />

                    <p:panelGrid columns="2">
                        <p:outputLabel value="Nome:" />
                        <p:inputText id="Nome" value="#{iu01_13MBean.venda.nome}" />
                        <p:outputLabel value="Data de venda:" />
                        <p:calendar id="Registro" value="#{iu01_13MBean.venda.data}" />
                        <p:outputLabel value="Valor:" />
                        <p:inputText id="Valor" value="#{iu01_13MBean.venda.valor}" />
                        <p:outputLabel value="Detalhes:" />
                        <p:inputText id="Detalhes" value="#{iu01_13MBean.venda.detalhes}" />
                        <p:outputLabel value="Produto:" />
                        <p:panelGrid columns="2">
                            <p:commandButton actionListener="#{iu01_08MBean.updateList}"
                                value="Selecionar produto" onclick="PF('selecionar_produto_WV').show();" />
                            <p:inputText id="Produto" value="#{iu01_08MBean.produto.nome}" />
                        </p:panelGrid>
                        <p:commandButton value="FINALIZAR" actionListener="#{iu01_13MBean.salvar}"
                    update="iu01_13_01_form, iu01_13_form"
                            onclick="PF('iu01_13_01').close();" />
                        <p:outputLabel />
                    </p:panelGrid>
        </p:dialog>
        <ui:include src="selecionar_produto.xhtml" />
 </h:form>
    </h:body>
 </html>

最新更新