0
votes

I have a problem with Primefaces 3.4.1. I need to get the selected value from a p:selectOneMenu. I have drained Google, but nothing help me. Here is my XHTML, Controller and Bean.

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. What I'm doing wrong?

2
I'm more familiar with Seam and Richfaces, but I think it's immediate="true", as it doesn't update the values in that case (it skips model update and validation). I'm not sure because usually you put immediate in the command button or the ajax control, but maybe Primefaces works that way.MaQy
Ok... I'd take off the immediate="true" but nothing changed.Tadashi
Can you post the whole xhtml file? Also use the browser inspector console to see what is being set to the server on submission.MaQy
Ok I've edited, and posted the whole xhtml code... even all is working well.. is just that p:SelectOneMenu...Tadashi

2 Answers

0
votes

Check the ids of the Cliente entities returned by your getClientes() method. 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.

0
votes

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. The best way to troubleshoot this kind of situations is checking what's sent in the AJAX request with the inspector.