Delete registration form, JSF Primefaces

Asked

Viewed 1,043 times

1

I’m using JSF with primefaces. I have a registration screen, and I have a button to delete the form data, would be a button to clear the form to make another registration. However, this wipe button is not working. I would like to ask for help.

Page code:

 <h:form id="formUser">         
          <p:messages id="messages" />            
           <p:panelGrid id ="panelGrid" columns="2" style="horizontal-align:center">           
                  <p:outputLabel for="id" value="ID:" />
                  <p:spinner id="id" value="#{UsuarioMB.usuario.id}" />
                   
                  <p:outputLabel for="nome" value="Nome:" />
                  <p:inputText id="nome" value="#{UsuarioMB.usuario.nome}" />                   
                     
                  <p:outputLabel for="senha" value="Senha:" />
                  <p:inputText id="senha" value="#{UsuarioMB.usuario.senha}" />  
                  
                  <p:outputLabel for="descricao" value="Descrição:" />
                  <p:inputTextarea id="descricao" value="#{UsuarioMB.usuario.descricao}" /> 
                  
   				  <p:outputLabel for="dataCadastro" value="DataCadastro:" />
                      <p:calendar value="#{UsuarioMB.usuario.dataCadastro}" locale="pt_BR"
				         id="dataCadastro" showButtonPanel="true">
				       <f:convertDateTime pattern="dd/MM/yyyy" />
			      </p:calendar>			         
			         
   	            <p:commandButton id="insert"  value="Cadastrar"   action= "#{UsuarioMB.cadastraUsuario}"   update="userTabela,:formUser"   >
                   </p:commandButton> 
                    <p:commandButton  value="Consultar" icon="ui-icon-star"  action= "#{UsuarioMB.consultar}"   update="userTabela"   >
                    </p:commandButton> 
                    <p:commandButton  value="Limpar"  action= "#{UsuarioMB.limpar}"  update=":formUser"  type="reset"    >
                   </p:commandButton> 
            </p:panelGrid> 
             
      <p:dataTable id="userTabela" var="usuario" value="#{UsuarioMB.lista}"
		paginator="true" rows="10" emptyMessage="Não há registros na lista"
		paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
		rowsPerPageTemplate="10,15,25,50,100"  rowKey="#{usuario.id}" selection="#{usuario}" selectionMode="single" >	
		<f:facet name="header">Lista de usuários</f:facet>	
	 	    <p:ajax event="rowSelect" listener="#{UsuarioMB.onRowSelect}" update=":formUser" /> 
	 	    
		<p:column headerText="ID" style="width: 10%;" sortBy="#{usuario.id}"  >
				 <h:outputText value="#{usuario.id}" />  
		</p:column>
		<p:column headerText="Nome" style="width: 25%;" sortBy="#{usuario.nome}"> 
               <h:outputText value="#{usuario.nome}" />  
		</p:column>
		<p:column headerText="Descrição" style="width: 25%;" sortBy="#{usuario.descricao}">
			<h:outputText value="#{usuario.descricao}" />
		</p:column>
		<p:column headerText="Data de Cadastro" style="width: 25%;" sortBy="#{usuario.dataCadastro}">
			<h:outputText value="#{usuario.dataCadastro}" />
		</p:column>  		
	</p:dataTable>              
   </h:form>     

Java code:

     public void limpar() {
 		System.out.println("Limpar");
 		System.out.println(usuario);
 		usuario = new Usuario();
 		setUsuario(usuario);	
 	}
        
     public void setUsuario(Usuario usuario) { 
    	 this.usuario = usuario;
     }
     

Thank you guys.

  • It was supposed to work the way you implemented it. How does the screen appear after pressing clean? Try to give an F5 after pressing it to see if it is problem in the ajax reRender.

3 answers

1

Do you want this wipe button only to erase the data or after entering the data the screen should be cleaned for new data? If the case is to clean after inserting, isn’t it better if you automate this process? In the registration methodUsuario you put a setUsuario(new User) and then after registering it will wipe the data automatically and with the update already cleaned it. Now if it’s just to wipe the data, try using the Clear process="@this button".

  • It can also instead of setting a new user, put user = null;

1

To simplify, you can set the user to null, as in the example below and change your update to xhtml:

public void limpar() {
        usuario = null;
    }


<p:commandButton  value="Limpar"  action= "#{UsuarioMB.limpar}"  update="@this"  type="reset"    >
                   </p:commandButton> 
  • If you set it to null it will nullpointer because the form calls data of this object without testing nullity. It has to be a "new User()" same.

1

Thanks guys, all the tips worked. I did so.

  <p:commandButton  value="Limpar"  action= "#{UsuarioMB.limpar}"  update=":formUser"   process="@this"  >
                   </p:commandButton> 

 if (dao.insertUsuario(usuario)) {
                    	setUsuario(new Usuario());
                	listar(); 
                     FacesContext.getCurrentInstance().addMessage(
                               null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Sucesso! Usuário cadastrado com sucesso!", "Usuário cadastrado com sucesso!"));
                } 

I appreciate the help, ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.