从p:SelectOneMenu中获取值

时间:2013-06-19 22:40:31

标签: java primefaces primefaces-extensions

我对Primefaces 3.4.1有疑问。我需要从p:selectOneMenu中获取所选值。我已经耗尽了谷歌,但没有任何帮助我。这是我的XHTML,Controller和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>

控制器:

   @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;
    ...

Combo中的列表填充良好,但在选择值时,该方法只返回零。我做错了什么?

2 个答案:

答案 0 :(得分:0)

检查Cliente方法返回的getClientes()个实体的ID。看起来id实体中的Cliente(在您的选择菜单中是itemValue)似乎没有从数据库返回,并且默认为零。

答案 1 :(得分:0)

据我所知,您的组件周围没有任何形式,因此不会提交。即使使用p:ajax,您也需要一个表单来将内容发送到服务器。解决此类情况的最佳方法是使用检查器检查AJAX请求中发送的内容。