简体   繁体   English

从ap:SelectOneMenu获取值

[英]Get the value from a p:SelectOneMenu

I have a problem with Primefaces 3.4.1. 我对Primefaces 3.4.1有问题。 I need to get the selected value from ap:selectOneMenu. 我需要从ap:selectOneMenu获取选定的值。 I have drained Google, but nothing help me. 我已经用尽了Google,但没有任何帮助。 Here is my XHTML, Controller and Bean. 这是我的XHTML,Controller和Bean。

XHTML: XHTML:

   <?xml version="1.0" encoding="ISO-8859-1" ?>
   <!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:h="http://java.sun.com/jsf/html"
   xmlns:f="http://java.sun.com/jsf/core"
   xmlns:p="http://primefaces.org/ui">
   <h:head>
   <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Bem Vindo</title>
   </h:head>
   <h:body>
   <h:outputStylesheet library="primefaces-dot-luv" name="theme.css" /> 
        <p:layout fullPage="true">
        <p:layoutUnit position="west" size="200" header="Menu principal" resizable="true" closable="false" collapsible="true" >
            <h:form id="menuForm">
                <h:panelGrid>
                <h:panelGroup>
                    <p:commandButton value="Cadastrar Cliente"  actionListener="#{indexController.pagCliente}"  style="font-size: 15px"/> 
                    <p:commandButton value="Cadastrar Produto"  actionListener="#{indexController.pagProduto}"  style="font-size: 15px"/>
                </h:panelGroup>
                </h:panelGrid>
            </h:form>

        </p:layoutUnit>
        <p:layoutUnit position="north" size="110" header="Selecionar Cliente" resizable="true" closable="false" collapsible="true" >
                     <p:selectOneMenu value="#{clienteController.idClient}" effect="fade" id="combo">
                        <f:selectItem itemLabel="Cliente" itemValue=""  />
                        <f:selectItems value="#{clienteController.clientes}"
                                     var="cli"
                                     itemLabel="#{cli.nome}"
                                     itemValue="#{cli.id}" />
                         <p:ajax listener="#{indexController.hideValida()}" update=":outPanel" />
                    </p:selectOneMenu>

        </p:layoutUnit>
        <p:layoutUnit position="center" header="Carrito" id="layoutCenter">
        <p:outputPanel id="outPanel" autoUpdate="true">
            <h:form id="form" rendered="#{indexController.hide}">
                        <p:dataTable id="tabelaVenda" var="vendas" value="#{clienteController.listarVendas}" style="font-size: 13px" 
                        sortBy="#{clienteController.listaSale}" sortOrder="descending">
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Produto"/>
                                </f:facet>
                                <h:outputText value="#{vendas.nomeProduto}" />
                            </p:column>
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="R$ Unitario"/>
                                </f:facet>
                                <h:outputText value="#{vendas.valorUnitario}"/>
                            </p:column>
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Quantidade"/>
                                </f:facet>
                                <h:outputText value="#{vendas.quantidadeVenda}"/>
                            </p:column>
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Preço Total (R$)"/>
                                </f:facet>
                                <h:outputText value="#{vendas.valorTotal}"/>
                            </p:column>
                            <p:summaryRow>  
                        <p:column colspan="3" style="text-align:right">  
                            Total:  
                        </p:column>  

                        <p:column>  
                            #{clienteController.total} R$  
                        </p:column>  
                        <p:column rendered="false"/>  
                    </p:summaryRow>  
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Excluir"/>
                                </f:facet>
                                <h:commandLink action="#{clienteController.excluirProdutoVenda}" value="Cancelar Produto"/>
                            </p:column>
                        </p:dataTable>
                        <p:commandLink value="Agregar Produto" actionListener="#{clienteController.prepararAdicionar}" 
                             style="font-size: 13px" update=":dialogForm:infosProduct" oncomplete="dialogSelectProd.show()"/>
                             <p:commandButton value="Finalizar Venda"  actionListener="#{clienteController.adicionarVenda}"  
                             style="font-size: 70%; float: right; vertical-align: bottom;" oncomplete="dialogPDFfactura.show()"/>
            </h:form>
            </p:outputPanel>
        </p:layoutUnit>
</p:layout>


<p:dialog header="Selecionar Produto" widgetVar="dialogSelectProd"  resizable="false" modal="true" showEffect="slide" width="500" >
        <h:form id="dialogForm" style="font-size: 13px">
            <h:panelGrid id="infosProduct" columns="2" style="margin-bottom:10px">
                <p:dataTable id="tabela" var="produto" value="#{clienteController.listarProdutos}" style="font-size: 13px" rowKey="#{produto.nomeProduto}"
                selection="#{clienteController.produto}" selectionMode="single">
                    <p:column>
                        <f:facet name="header">
                            <h:outputText value="Produto"/>
                        </f:facet>
                        <h:outputText value="#{produto.nomeProduto}" />
                    </p:column>
                    <p:column>
                        <f:facet name="header">
                            <h:outputText value="Quantidade Disponível"/>
                        </f:facet>
                        <h:outputText value="#{produto.quantidade}"/>
                    </p:column>
                    <p:column>
                        <f:facet name="header">
                            <h:outputText value="Valor Unitario"/>
                        </f:facet>
                        <h:outputText value="#{produto.valor}"/>
                    </p:column>
                </p:dataTable>

                <h:outputLabel for="quantidadeVenda" value="Quantidade do Pedido: " />
                <h:inputText id="quantidadeVenda" value="#{clienteController.venda.quantidadeVenda}"/>

                <p:commandButton update=":form:tabelaVenda" oncomplete="dialogSelectProd.hide();" actionListener="#{clienteController.adicionarVendaTemp}" value="Adicionar no Carrinho"/>
            </h:panelGrid>
        </h:form>
    </p:dialog>

    <p:dialog header="Fatura" widgetVar="dialogPDFfactura" modal="true" showEffect="slide" >
         <p:lightBox styleClass="PDFbox">  
                <p:media value="/pdf/eduardo.pdf" width="100%">
                    Your browser can't display pdf, <h:outputLink value="/pdf/eduardo.pdf">click</h:outputLink> to download pdf instead.
                 </p:media>
            </p:lightBox>  
     </p:dialog>

Controller: 控制器:

   @ManagedBean
   @SessionScoped
   public class ClienteController {

      private Cliente cliente;
      private List<Cliente> clientes;
      ...
      private long idClient; 

   public long getIdClient() {
    return idClient;
}

public void setIdClient(long idClient) {
    this.idClient = idClient;
}

public List<Cliente> getClientes() {
    clientes = ClienteDaoImp.lista();
    return clientes;
}

public void setClientes(List<Cliente> clientes) {
    this.clientes = clientes;
}

Cliente class: 客户类:

   @Entity
   public class Cliente implements Serializable {

    @Id
    @GeneratedValue
    private long id;
    private String nome;
    private String endereco;
    ...

The list in Combo is well populated, but when selecting a value, the method just return me a zero. Combo中的列表填充良好,但是当选择一个值时,该方法仅向我返回零。 What I'm doing wrong? 我做错了什么?

Check the ids of the Cliente entities returned by your getClientes() method. 检查您的getClientes()方法返回的Cliente实体的ID。 It looks like the id in the Cliente entity (which is the itemValue in your select menu) isn't being returned from the database, and is defaulting to zero. 看起来好像没有从数据库返回Cliente实体中的id (这是您选择菜单中的itemValue ),并且默认为零。

As far as I can see, you don't have any form around your component, so it won't get submitted. 据我所知,您的组件周围没有任何表格,因此不会提交。 Even with p:ajax you need a form to send the content to the server. 即使使用p:ajax您也需要使用表格将内容发送到服务器。 The best way to troubleshoot this kind of situations is checking what's sent in the AJAX request with the inspector. 解决此类情况的最佳方法是与检查器一起检查AJAX请求中发送的内容。

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

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