Alterar dados no jsf com primefaces

9 respostas
C

Galera tenh uma tabela com um commandLink quando o cara clica nele abro um p:dialog para fazer a edição do registro mas naum funcionou

<p:column>
                                <p:commandLink value="Editar" onclick="panelEditar.show();" >
                                    <c:setPropertyActionListener value="#{comb}"
                                                                 target="#{combustivelController.combustivel}" />
                                </p:commandLink>
                            </p:column>

ManagedBean

@ManagedBean
@ViewScoped
public class CombustivelController implements Serializable {
    
    private Combustivel combustivel;
    private List<Combustivel> listaCombustiveis = null;
    
    @PostConstruct
    public void inicializa()
    {
        try {
            combustivel = new Combustivel();
            listaCombustiveis = new CombustivelBO().buscarTodosCombustiveis();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void salvarCombustivel()
    {
        try {
            new CombustivelBO().salvar(this.combustivel);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Gravado", "Combustível gravado com sucesso."));
            combustivel = new Combustivel();
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Erro", "Não foi possível gravar o combustível, tente novamente."));
        }
    }
    
    public Combustivel getCombustivel()
    {
        return this.combustivel;
    }
            
    public void setCombustivel(Combustivel c)
    {
        this.combustivel = c;
    }

    public List<Combustivel> getListaCombustiveis() {
        return listaCombustiveis;
    }

    public void setListaCombustiveis(List<Combustivel> listaCombustiveis) {
        this.listaCombustiveis = listaCombustiveis;
    }
    
}

alguma ideia de por que naum esta funcionando obrigado pela ajuda

9 Respostas

D

Ola amigo

tente mudar no botao de onclick pra oncomplete

<p:commandLink value="Editar" oncomplete="panelEditar.show();" process="@this"> <c:setPropertyActionListener value="#{comb}" target="#{combustivelController.combustivel}" /> </p:commandLink>
qualquer coisa posta o codigo do dialog tambem

C

Não funcionou infelizmente

&lt;h:body&gt;
        &lt;f:composition&gt;
            &lt;h:form styleClass="well form-horizontal" id="formCadCombustivel" style="margin-left: -20px;"&gt;
                &lt;fieldset&gt;
                    &lt;legend&gt;Cadastro Combustivel&lt;/legend&gt;
                    
                    &lt;p:growl id="growl" showDetail="true" sticky="true" life="10000"/&gt;
                    
                    &lt;div class="control-group" style="padding-top: 40px;"&gt;
                        &lt;h:outputLabel value="Combustível" styleClass="control-label" /&gt;
                        &lt;div class="controls"&gt;
                            &lt;p:inputText required="true" requiredMessage="Campo Combustível é requerido" 
                                         styleClass="input input-large" 
                                         value="#{combustivelController.combustivel.dsCombustivel}" /&gt;
                        &lt;/div&gt;
                        
                    &lt;/div&gt;
                    
                    &lt;div class="form-actions" style="margin-top: 60px;"&gt;
                        &lt;p:commandButton type="submit" value="Salvar" styleClass="btn btn-primary" 
                                         update="@form @this growl" 
                                         actionListener="#{combustivelController.salvarCombustivel()}"/&gt;
                        &lt;p:commandButton type="reset" value="Limpar" styleClass="btn" 
                                         style="margin-left: 20px;"/&gt;
                    &lt;/div&gt;
                &lt;/fieldset&gt;
            &lt;/h:form&gt;
        &lt;/f:composition&gt;
    &lt;/h:body&gt;
&lt;p:dialog modal="true" header="Editar" showEffect="fade" hideEffect="explode" 
                                  widgetVar="panelEditar" &gt;
                            &lt;f:insert&gt;
                                &lt;f:include src="../cadastros/forms/formCadCombustivel.xhtml"/&gt;
                            &lt;/f:insert&gt;
                        &lt;/p:dialog&gt;
D

Amigo o que acontece quando vc clica pra abrir o dialog?
ele simplesmente nao abre ou abre sem ter o objeto setado pq talvez vc teria que fazer um update no conteudo dentro do seu dialog mas como vc esta chamando outra pagina com o insert talvez esse update nao funcione, tente colocar sua pagina dentro de algum componente do primefaces como um panel e dar um update nesse panel assim:

<p:commandLink value="Editar" oncomplete="panelEditar.show();" process="@this" update="conteudo"> <c:setPropertyActionListener value="#{comb}" target="#{combustivelController.combustivel}" /> </p:commandLink>

<p:dialog modal="true" header="Editar" showEffect="fade" hideEffect="explode" widgetVar="panelEditar" > <p:panel id="conteudo"> <f:insert> <f:include src="../cadastros/forms/formCadCombustivel.xhtml"/> </f:insert> </p:panel> </p:dialog>

Abraços

C

Nao funcionou amigo sera que naum teria mas nada que pudesse ser feito?

D

Amigo o dialog chega a ser aberto?
Se sim a pagina chega a ser renderizada dentro dele?
Se sim o objeto combustivel chega a ser setado?

Abraços

C

Amigo mostra o dialog, seta o combustivel corretamente no meu bean so naum mostra no campo da tela, talvez falte fazer um update naum sei como, alguma ideia?

D

Entao amigo provavelmente falta fazer um update mas eu nunca chamei uma pagina dentro de um dialog sempre usei campos diretos dentro dele dai eu simplesmente dava um update=":formDentroDoDialog" e mostrava certo alem do oncomplete pra chamar o dialog.
Não sei se no seu caso é viavel mas tenta colocar os campos direto no dialog ao inves de usar o insert e fazer o update do form eu sempre usei assim e funcionou

Abraços

M

Blz, conseguiu resolver???

Tem um exemplo utilizando primefaces…
fiz isso

<p:commandButton action="#{clienteBean.carregaForm(clientes)}" icon=“ui-menuitem-icon ui-icon ui-icon-arrowrefresh-1-w” title=“Alterar” onclick=“confirmation.hide()”/>

No meu ClienteBean, criei esse método.

public void carregaForm(Cliente c) {

this.cliente= c;

}

Abraços…

J

Olá Pessoal, boa noite,

ressucitei o post porque estou com o mesmo problema e a solução dada aqui para mim não resolveu. As p:dialog simplesmente não são exibidas.

Não se assustem com a tristeza que é o código. Estou aprendendo essas tecnologias há 2 semanas. Tenham piedade.

Peço humildemente e encarecidamente aos experts de plantão, no dia do programador, para me ajudarem. Fico-lhes muitíssimo agradecido.

Trata-se de um pequeno sistema CRUD, com Hibernate, JSF 2, com Apache Tomcat 6 e JDK 1.6. Estas configurações não podem ser alteradas.

Seguem os códigos:

package controller;  
  
import dao.ImovelDao;  
  
import java.io.Serializable;  
import java.util.List;  
import javax.faces.bean.ManagedBean;  
import javax.faces.bean.ViewScoped;  
import javax.faces.model.DataModel;  
import javax.faces.model.ListDataModel;  
  
  
  
  
import model.Imovel;  
  
@ManagedBean @ViewScoped  
public class ImovelBean implements Serializable {  
    private ImovelDao imovelDao;  
    private Imovel imovel = new Imovel();  
    private DataModel<Imovel> imoveis;  
  
    public void novo() {  
        imovel = new Imovel();  
    }  
  
    public String inserir() {  
        String resultado = "falha";  
        imovelDao = new ImovelDao();  
        boolean retorno = imovelDao.inserir(imovel);  
  
        if (retorno) {  
            resultado = "imoveis";  
        }  
  
        return resultado;  
    }  
  
    public void selecionar() {  
        imovel = imoveis.getRowData();  
    }  
  
    public String alterar() {  
        String resultado = "falha";  
        imovelDao = new ImovelDao();  
        boolean retorno = imovelDao.alterar(imovel);  
  
        if (retorno) {  
            resultado = "imoveis";  
        }  
  
        return resultado;  
    }  
  
    public String remover() {  
        String resultado = "falha";  
        imovelDao = new ImovelDao();  
        boolean retorno = imovelDao.remover(imovel);  
  
        if (retorno) {  
            resultado = "imoveis";  
        }  
  
        return resultado;  
    }  
  
    public DataModel<Imovel> consultar() {  
        imovelDao = new ImovelDao();  
        List<Imovel> imovelList = imovelDao.listar();  
        imoveis = new ListDataModel<Imovel>(imovelList);  
        return imoveis;  
    }  
      
    public Imovel getImovel() {  
        return imovel;  
    }  
      
    public void setImovel(Imovel imovel) {  
        this.imovel = imovel;  
    }  
  
    public DataModel<Imovel> getImoveis() {  
        imovelDao = new ImovelDao();  
        List<Imovel> imovelList = imovelDao.listar();  
        imoveis = new ListDataModel<Imovel>(imovelList);  
        return imoveis;  
    }  
  
    public void setImoveis(DataModel<Imovel> imoveis) {  
        this.imoveis = imoveis;  
    }  
}
<!DOCTYPE html>  
<html xmlns="http://www.w3.org/1999/xhtml"  
    xmlns:f="http://xmlns.jcp.org/jsf/core"  
    xmlns:h="http://xmlns.jcp.org/jsf/html"  
    xmlns:p="http://primefaces.org/ui">  
  
<h:head>  
    <title>Gerenciar Imóveis</title>  
</h:head>  
<h:body>  
    <div align="center">  
        <h:form id="formPrincipal">  
  
            <p:dataTable id="tabelaImoveis" var="lista" value="#{imovelBean.imoveis}"   
                 selectionMode="single" selection="#{imovelBean.imovel}"   
                 rowKey="#{lista.matriculaImovel}" >  
                 <p:ajax event="rowSelect" update=":formAlterar" />  
                <f:facet name="header">Gerenciador de Imóveis</f:facet>  
                <p:column headerText="Matrícula">  
                    <h<img src="http://javafree.uol.com.br/forum/images/smiles/icon_surprised.gif">utputText value="#{lista.matriculaImovel}" />  
                </p:column>  
                <p:column headerText="Endereço">  
                    <h<img src="http://javafree.uol.com.br/forum/images/smiles/icon_surprised.gif">utputText value="#{lista.enderecoImovel}" />  
                </p:column>  
                <p:column headerText="Valor do Imóvel">  
                    <h<img src="http://javafree.uol.com.br/forum/images/smiles/icon_surprised.gif">utputText value="#{lista.valorImovel}">  
                        <f:convertNumber type="currency" locale="pt_BR" />  
                    </h<img src="http://javafree.uol.com.br/forum/images/smiles/icon_surprised.gif">utputText>  
                </p:column>  
                <p:column headerText="Ações">  
                    <p:commandLink title="Alterar" update=":formAlterar:Alterar"  
                        oncomplete="dialogAlterar.show()" immediate="true">  
                        <p:graphicImage value="./imagens/editar.jpg" />  
                        <f:setPropertyActionListener target="#{imovelBean.imovel}"  
                            value="#{lista}" />  
                    </p:commandLink>  
                    <p:commandLink title="Excluir" update=":formExcluir:Excluir"  
                        oncomplete="confirmation.show()" immediate="true">  
                        <p:graphicImage value="./imagens/del.jpg" />  
                        <f:setPropertyActionListener target="#{imovelBean.imovel}"  
                            value="#{lista}" />  
                    </p:commandLink>  
                </p:column>  
  
            </p:dataTable>  
            <p:commandButton value="Cadastrar" id="cadastrar"  
                oncomplete="dialogCadastrar.show()" />  
        </h:form>  
    </div>  
    <h:form id="formCadastrar">  
        <p:dialog header="Novo Registro" widgetVar="dialogCadastrar" modal="true">  
            <p:panelGrid id="panelCadastrar" columns="2">  
                <h<img src="http://javafree.uol.com.br/forum/images/smiles/icon_surprised.gif">utputText value="Matrícula" />  
                <p:inputText value="#{imovelBean.imovel.matriculaImovel}" />  
                <h<img src="http://javafree.uol.com.br/forum/images/smiles/icon_surprised.gif">utputText value="Endereço:" />  
                <p:inputText value="#{imovelBean.imovel.enderecoImovel}" />  
                <h<img src="http://javafree.uol.com.br/forum/images/smiles/icon_surprised.gif">utputText value="Valor:" />  
                <p:inputText value="#{imovelBean.imovel.valorImovel}" />  
            </p:panelGrid>  
            <p:commandButton value="Cadastrar"  
                actionListener="#{pessoaBean.cadastrar}"  
                update=":formPrincipal:tabelaImoveis"  
                oncomplete="dialogCadastrar.hide()" />  
        </p:dialog>  
    </h:form>  
    <h:form id="formAlterar">  
        <p:dialog header="Alterar Registro" widgetVar="dialogAlterar" modal="true" id="Alterar" dynamic="true">  
            <p:panelGrid id="panelAlterar" columns="2">  
                <h<img src="http://javafree.uol.com.br/forum/images/smiles/icon_surprised.gif">utputText value="Matrícula" />  
                <p:inputText value="#{imovelBean.imovel.matriculaImovel}" />  
                <h<img src="http://javafree.uol.com.br/forum/images/smiles/icon_surprised.gif">utputText value="Endereço:" />  
                <p:inputText value="#{imovelBean.imovel.enderecoImovel}" />  
                <h<img src="http://javafree.uol.com.br/forum/images/smiles/icon_surprised.gif">utputText value="Valor:" />  
                <p:inputText value="#{imovelBean.imovel.valorImovel}" />  
            </p:panelGrid>  
            <p:commandButton value="Alterar"  
                actionListener="#{imovelBean.alterar}"  
                update=":formPrincipal:tabelaImoveis"  
                oncomplete="dialogAlterar.hide()" />  
        </p:dialog>  
    </h:form>  
    <h:form id="formExcluir">  
        <p:confirmDialog id="Excluir"  
            message="Confirma exclusão do registro do imóvel de matrícula #{imovelBean.imovel.matriculaImovel}?"  
            header="Excluir" severity="alert" widgetVar="confirmation">  
              
            <p:commandButton id="confirm" value="Sim"  
                oncomplete="confirmation.hide()"  
                actionListener="#{imovelBean.excluir}"  
                update=":formPrincipal:tabelaImoveis" />  
            <p:commandButton id="decline" value="Não"  
                onclick="confirmation.hide()" type="button" />  
        </p:confirmDialog>  
    </h:form>  
</h:body>  
</html>
Criado 25 de abril de 2012
Ultima resposta 13 de set. de 2014
Respostas 9
Participantes 4