Exclusão, Edição e Insert no primefaces

1 resposta
F

Meu problema e o seguinte
Quando eu clico no botão editar que eu criei na coluna do dataTable os parametros para o dialog que eu estou abrindo não sao repassados, ja vi varios exemplos e ja tentei de tudo quanto e forma e nada.
Segue os arquivos.

index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3c.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    
    <h:head>
        <link type="text/css" rel="stylesheet" href="resources/estilos.css" />
        <title>Facelet Title</title>                 
    </h:head>
    
    <body>         
        <p:growl id="mensagens" showDetail="false" life="3000"/>              
        <h:form>
            <p:menubar>              
                <p:submenu label="File">
                    <p:menuitem value="About" url="#" />  
                    <p:separator />  
                    <p:menuitem value="Sair" url="#" />  
                </p:submenu>
            
                <p:submenu label="Cadastros" >
                    <p:menuitem value="Estados" onclick="f_dlg_estado.show()"/>
                </p:submenu>
            </p:menubar>                        
        </h:form>            
     <ui:include src="estados.xhtml" />        
    </body>
    </html>
estados.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<ui:composition 
      xmlns="http://www.w3c.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core">
          
        <p:dialog id="id_dlg_cadestado" widgetVar="f_dlg_cadestado" 
                  header="Dados" resizable="false" 
                  showEffect="fold" hideEffect="fold"
                  closable="true"
                  modal="true">                       
            
            <h:form id="form_cad_estado">
                <p:panelGrid columns="2">
                    <h:outputText value="Estado" class="negrito" />
                    <p:inputText value="#{estadosMB.estados.sigla}"  id="sigla" maxlength="2" />
                    
                    <h:outputText value="Nome" class="negrito" />
                    <p:inputText value="#{estadosMB.estados.estado}" id ="estado" maxlength="50"/>
                </p:panelGrid>
                    
                <p:commandButton value="Salvar" update=":mensagens,form_cad_estado" 
                                 actionListener="#{estadosMB.gravaEstados()}"
                                oncomplete="handleGravaRequest(xhr, status, args)" />
                <p:commandButton type="button" value="Cancelar" onclick="f_dlg_cadestado.hide();"/>
            </h:form>
        </p:dialog>        

            <p:dialog id="id_dlg_estado" widgetVar="f_dlg_estado" height="400" width="600"
                      header="Cadastro de Estados" resizable="false" closable="true"
                      showEffect="fold" hideEffect="fold" >                           
                
                <h:form id="form_estado" >                                 
                    <p:dataTable id="dt_estados" var="v_dt_estados" value="#{estadosMB.listarEstados}" 
                                 rows="12" paginator="true">  
                        
                        <p:column headerText="Estado" id="sigla" style="width:15%;text-align: center">                         
                            <h:outputText value="#{v_dt_estados.sigla}" />  
                        </p:column>         
                    
                        <p:column id="estado" headerText="Nome" >
                            <h:outputText value="#{v_dt_estados.estado}" />
                        </p:column>  
                    
                        <p:column headerText="Ações" id="c_acoes" style="width:10%;text-align: center">
                            
                            <p:commandLink title="Editar"
                                           oncomplete="f_dlg_cadestado.show()">  
                                <p:graphicImage value="imagens/acoes/edit.png"/>   
                                <f:setPropertyActionListener target="#{estadosMB.estados}" value="#{v_dt_estados}" />  
                            </p:commandLink>                                                          
                            
                            <p:commandLink update="dt_estados" title="Excluir">  
                                <p:graphicImage value="imagens/acoes/delete.png"/>   
                                <f:setPropertyActionListener target="#{estadosMB.estados}" value="#{v_dt_estados}"/>  
                            </p:commandLink>                              
                                                                       
                        </p:column>
                        
                    </p:dataTable>        
                </h:form>  
                <f:facet name="footer">
                    <p:commandButton type="button" value="Novo" onclick="f_dlg_cadestado.show()"/>
                    <p:commandButton type="button" value="Fechar" onclick="f_dlg_estado.hide()"/>
                </f:facet>                   
        </p:dialog>
    
    
       
        
              
            <script type="text/javascript">  
                function handleGravaRequest(xhr, status, args) {
                    if( args.sucesso == false ) {  
                        jQuery('#id_dlg_cadestado').effect("shake", { times:3 }, 100);  
                     } else {  
                         f_dlg_cadestado.hide();  
                     }
                 }  
            </script>
     
</ui:composition>
EstadosMB.Java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controles;

import entidades.HibernateUtil;
import entidades.Estados;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import org.hibernate.Session;
import org.primefaces.context.RequestContext;
import javax.faces.bean.ViewScoped; 

/**
 *
 * @author Vicente B.Leonel
 */
@ManagedBean
@ViewScoped 
public class EstadosMB implements Serializable {

    private Estados estados = new Estados();
    private List<Estados> listarEstados;

    public EstadosMB() {
    }

    public Estados getEstados() {
        return estados;
    }

    public void setEstados(Estados estados) {
        this.estados = estados;
    }
    
    private String gravar() throws Exception {

        String retorno = "Dados gravados com sucesso...";

        Session session = HibernateUtil.getSession();
        try {
            session.beginTransaction().begin();
            session.saveOrUpdate(estados);
            session.getTransaction().commit();
        } catch (Exception erro) {
            retorno = erro.toString();
        } finally {
            session.close();
        }
        return retorno;
    }       

    public void gravaEstados( ) {
        
       String msg = "";       
       boolean gravou = false;
         if (estados.getSigla().isEmpty()) {
            msg = "Favor informar a sigla do estado";
        } else if (estados.getEstado().isEmpty()) {
            msg = "Favor informar o nome do estado";
        } else {
            try {
                msg = gravar();                 
                gravou = true;
                this.estados = new Estados();
            } catch (Exception ex) {
                Logger.getLogger(EstadosMB.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        FacesContext.getCurrentInstance().
                addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO,
                msg , null));
        
        RequestContext requestContext = RequestContext.getCurrentInstance();                 
        requestContext.addCallbackParam("sucesso", gravou );  
   
    }

    public List<Estados> getListarEstados() {
        Session session = HibernateUtil.getSessionFactory().openSession();  
        session.beginTransaction().begin();  
        listarEstados = session.createQuery("from Estados order by sigla").list(); 
        session.getTransaction().commit();
        return listarEstados;
    }

    public void setListarEstados(List<Estados> listarEstados) {
        this.listarEstados = listarEstados;
    }
}

se alguem puder me dar uma luz serei grato
uso o primefaces 3.2

1 Resposta

F

Ninguém ???

Criado 27 de abril de 2012
Ultima resposta 30 de abr. de 2012
Respostas 1
Participantes 1