【问题标题】:Datatable inside dialog is not sending data to bean对话框内的数据表未向 bean 发送数据
【发布时间】:2017-01-31 17:44:06
【问题描述】:

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

我只想制作一个表格,当用户选择一行并单击 Selecionar 按钮时,对象 project 将发送到主 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>

【问题讨论】:

    标签: jsf primefaces datatable xhtml javabeans


    【解决方案1】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-14
      • 2016-08-25
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      • 2011-05-25
      • 2012-06-21
      • 1970-01-01
      相关资源
      最近更新 更多