[RESOLVIDO] Limpar campos do form depois do cadastro

3 respostas
lucasbf1992

Boa Noite, não estou conseguindo limpar os campos da view apos realizar um cadastro, já tentei setar null nos objetos, obter uma nova instancia do objeto e nada. Alguém pode me ajudar, segue o codigo abaixo

BEAN
package br.com.sistema_servicos.bean;

import br.com.sistema_servicos.dao.DAOMotivos;
import br.com.sistema_servicos.entity.Motivo;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Lucas
 */
@ManagedBean
@SessionScoped
public class MotivosBean {

     private Motivo motivo = new Motivo();
    private DAOMotivos dao = new DAOMotivos();
    private List<Motivo> listaMotivos;
    private String busca;

    public MotivosBean() {
    }
    
    

    public Motivo getMotivo() {
        return motivo;
    }

    public void setMotivo(Motivo motivo) {
        this.motivo = motivo;
    }

    public String getBusca() {
        return busca;
    }

    public void setBusca(String busca) {
        this.busca = busca;
    }
    
    

    public String adicionarMotivo() {

        dao.addMotivos(motivo);
        motivo.setDescricao(null);
        return "motivos";
    }

    public List listarMotivos() {
        listaMotivos = dao.getList();
        return listaMotivos;

    }

    public String removerMotivo(Motivo m) {
        this.motivo = m;
        dao.removeMotivo(m);
        

        return "motivos";
    }

    public String carregarMotivos(Motivo m) {
        this.motivo = m;
        return "atualizarMotivo.xhtml";
    }

    public String atualizar() {
        dao.updateMotivo(motivo);
        motivo.setDescricao(null);


        return "motivos";
    }
    
     public List<String> complete(String query) {  
        List<String> results = new ArrayList<String>();  
        
        results= dao.getDescricao(query);
        
        return results;  
    }  
}
VIEW
<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:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">


    <h:head>
        <title>Gerenciar Motivos</title>
    </h:head>
    <h:body>
        <ui:composition template="template.xhtml">


            <ui:define name="corpo">
                <h:form>
                    <center>
                        <b> Gerenciar Motivos </b>
                        <p:panel header="Adicionar" toggleable="true" toggleOrientation="horizontal"> 
                            Descricao: <p:inputText id="descricaoTxt" value="#{motivosBean.motivo.descricao}"/>
                            <br/><br/>
                            <p:commandButton  icon="ui-icon-disk" id="btnEnviar" value="Cadastrar" action="#{motivosBean.adicionarMotivo}" update="tblmotivos" /> 
                        </p:panel>
              
                        <p:dataTable value="#{motivosBean.listarMotivos()}" var ="tabela" id="tblmotivos" paginator="true" rows="7">
                            <p:column headerText="ID" style="text-align:center">
                                <h:outputText value="#{tabela.idMotivo}" />
                            </p:column>
                            <p:column sortBy="descricao" headerText="Descrição" style="text-align:center">
                                <f:facet name="header">
                                    Descrição
                                </f:facet>
                                <h:outputText value="#{tabela.descricao}"/>
                            </p:column>

                            <p:column style="text-align:center">
                                <f:facet name="header">
                                    Ações
                                </f:facet>
                                <p:commandLink value="Remover" action="#{motivosBean.removerMotivo(tabela)}" update="tblmotivos"/>
                                |<p:commandLink value="Atualizar" action="#{motivosBean.carregarMotivos(tabela)}"/> 
                                     </p:column>
                        </p:dataTable>

                    </center>
                </h:form> 
            </ui:define>

        </ui:composition>

    </h:body>

</html>

3 Respostas

AmauriSpPoa

Alem de setar null nos objetos tem que colocar o form no update do botão e coloque um id no form.

lucasbf1992

vou realizar o teste aqui, muito obrigado cara

lucasbf1992

Cara funcionou certinho, muito obrigado pela atenção

Criado 9 de setembro de 2013
Ultima resposta 12 de set. de 2013
Respostas 3
Participantes 2