Como evitar o cadstro de 2 objetos no BD ao apertar F5 - jsf [Resolvido]

Boa Tarde!

Estou com um problema… tenho uma tela de cadastro: adiciona.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.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.prime.com.tr/ui"
	xmlns:h="http://java.sun.com/jsf/html">
<h:head>
</h:head>
<h:body>

<p:layout fullPage="true">
	<p:layoutUnit position="top" height="150" header="Sistema de cadastro de clientes - cadastro" resizable="false" closable="true" collapsible="true"></p:layoutUnit>

	<p:layoutUnit position="center">
		<h:form>
			<p>Nome: <p:inputText value="#{vendasBeans.cliente.nome}" style="position: absolute; left:150px"/></p>
			<p>Endereço: <p:inputText value="#{vendasBeans.cliente.endereco}" style="position: absolute; left:150px"/></p>
			<p>Numero: <p:inputText value="#{vendasBeans.cliente.numero}" style="position: absolute; left:150px"/></p>
			<p>Cnpj: <p:inputText value="#{vendasBeans.cliente.cnpj}" style="position: absolute; left:150px"/></p>
			<p>IE: <p:inputText value="#{vendasBeans.cliente.ie}" style="position: absolute; left:150px"/></p>
			<p>Contato: <p:inputText value="#{vendasBeans.cliente.contato}" style="position: absolute; left:150px"/></p>
			<p>Telefone: <p:inputText value="#{vendasBeans.cliente.telefone}" style="position: absolute; left:150px"/></p>
			<p>Email: <p:inputText value="#{vendasBeans.cliente.email}" style="position: absolute; left:150px"/></p><br/>
			
		<p:commandButton action="#{vendasBeans.adiciona}" value="Ok" ajax="false" style="position: absolute; left:215px"/>
		<p:commandButton action="#{vendasBeans.listando}" value="listar" ajax="false" style="position: absolute; left:315px" />
		</h:form>
	
	</p:layoutUnit>

		<p:layoutUnit position="bottom" height="150" resizable="false" closable="false" collapsible="true">
			<p>Bruno Marques</p>
		</p:layoutUnit>
	
	</p:layout>

</h:body>
</html>

e tenho a lista.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.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.prime.com.tr/ui"
	xmlns:h="http://java.sun.com/jsf/html">
<h:head>
</h:head>
<h:body>

<p:layout fullPage="true">
	
	<p:layoutUnit position="top" height="150" header="Sistema de cadastro de clientes - Lista de clientes" resizable="false" closable="true" collapsible="true">
	
	<a href="adiciona.xhtml"><p:button value="Adicionar" style="position:absolute; left: 1280px; margin-top:30px"></p:button></a>
	
	</p:layoutUnit>

<p:layoutUnit position="center">
<h:form>

<!-- Modal -->


<p:dialog header="Editar" widgetVar="singleCarDialog" resizable="false"
              width="500">

        <h:panelGrid id="displaySingle" columns="2" cellpadding="4">


            <h:outputText value="Nome:" />
            <h:inputText value="#{vendasBeans.cliente.nome}" />
            
            <h:outputText value="Endereço:" />
            <h:inputText value="#{vendasBeans.cliente.endereco}" />
            
            <h:outputText value="Numero:" />
            <h:inputText value="#{vendasBeans.cliente.numero}" />
            
            <h:outputText value="Cnpj:" />
            <h:inputText value="#{vendasBeans.cliente.cnpj}" />
            
            <h:outputText value="Ie:" />
            <h:inputText value="#{vendasBeans.cliente.ie}" />
            
            <h:outputText value="Contato:" />
            <h:inputText value="#{vendasBeans.cliente.contato}" />
            
            <h:outputText value="Telefone:" />
            <h:inputText value="#{vendasBeans.cliente.telefone}" />
            
            <h:outputText value="Email:" />
            <h:inputText value="#{vendasBeans.cliente.email}" />
      
      <p:commandButton action="#{vendasBeans.delete}" value="Deletar" onclick="singleCarDialog.close()"/>
      <p:commandButton action="#{vendasBeans.altera}" value="Alterar" onclick="singleCarDialog.close()"/>
	
        </h:panelGrid>
        
</p:dialog>


<!-- Fim do modal -->


<p:dataTable var="clientes" value="#{vendasBeans.clientes}"  
			 paginator="true" rows="10"
             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
             rowsPerPageTemplate="5,10,15" dynamic="true" selection="#{vendasBeans.cliente}" selectionMode="single">
	
	<p:column>
		<f:facet name="header">Nome</f:facet>
		<h:outputText value="#{clientes.nome}"/>
	</p:column>


	<p:column>
		<f:facet name="header">Endereço</f:facet>
		<h:outputText value="#{clientes.endereco}" />
	</p:column>


	<p:column>
		<f:facet name="header">Numero</f:facet>
		<h:outputText value="#{clientes.numero}" />
	</p:column>

	<p:column>
		<f:facet name="header">Cnpj</f:facet>
		<h:outputText value="#{clientes.cnpj}" />
	</p:column>

	<p:column>
		<f:facet name="header">IE</f:facet>
		<h:outputText value="#{clientes.ie}" />
	</p:column>

	<p:column>
		<f:facet name="header">Contato</f:facet>
		<h:outputText value="#{clientes.contato}" />
	</p:column>

	<p:column>
		<f:facet name="header">Telefone</f:facet>
		<h:outputText value="#{clientes.telefone}" />
	</p:column>

	<p:column>
		<f:facet name="header">Email</f:facet>
		<h:outputText value="#{clientes.email}" />
	</p:column>

</p:dataTable><br/>

	<p:commandButton value="Visualizar" image="ui-icon ui-icon-search"
	    update="displaySingle" oncomplete="singleCarDialog.show()"
	    style="position: absolute; left: 15px"/>
	
	</h:form>

</p:layoutUnit>

<p:layoutUnit position="bottom" height="110" resizable="false" closable="true" collapsible="true">
			<p>Bruno Marques</p>
		</p:layoutUnit>
</p:layout>




</h:body>
</html>

e meu managedBean:


package vendas.managedBeans;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import vendas.dao.vendasDao;
import vendas.pojo.Cliente;


@RequestScoped
@ManagedBean


public class vendasBeans {
	
	Cliente cliente = new Cliente();
	
	public List<Cliente> clientes;
	
	public vendasBeans() throws SQLException{
		listando();
	}
	
	
	public Cliente getCliente() {
		return cliente;
	}
	public void setCliente(Cliente cliente) {
		this.cliente = cliente;
	}
	public List<Cliente> getClientes() {
		return clientes;
	}
	public void setClientes(List<Cliente> clientes) {
		this.clientes = clientes;
	}
	

	public String adiciona() throws SQLException {
		
		vendasDao dao = new vendasDao();
		dao.inserir(cliente);
		cliente = null;
		
		return listando();
	}
	
	//lista
	
	public String listando() throws SQLException{
		
		vendasDao dao = new vendasDao();
		clientes = new ArrayList<Cliente>();
		clientes = dao.listar();
		cliente = new Cliente();
		
		return "lista";
		
	}
	
	//delete
	
	public String delete() throws SQLException{
		
		vendasDao dao = new vendasDao();
		dao.deletar(cliente.getId());
		clientes.remove(cliente);
		return listando();
	}
	
	//altera
	
	public String altera() throws SQLException{
			
			vendasDao dao = new vendasDao();
			dao.altera(cliente);
			return listando();
		}
		

}

o problema é: quando eu cadastro um cliente ele redireciona o mesmo pra lista, mas quando aperto F5 ele cria um novo objeto, ja tentei colocar a anotação @ViewScoped mas não deu certo, não sei mais oque faço :smiley:
espero que vcs me ajudem, Obrigado!

Cara, já tive esse problema também . veja essa parte do seu código:

[code]public String adiciona() throws SQLException {

    vendasDao dao = new vendasDao();  
    dao.inserir(cliente);  
    cliente = null;// ELE É UM OBJETO NÃO PODE SER PASSADO NULL PARA ELE  
      
    return listando();  
} [/code]

Faça assim:

[code]public String adiciona() throws SQLException {

    vendasDao dao = new vendasDao();  
    dao.inserir(cliente);  
    cliente = new Cliente(); //SERÁ CRIADO UM NOVO OBJETO DEPOIS DA INSERÇÃO ! 
      
    return listando();  
} [/code]

brunozm, cara web é assim mesmo, é como se vc estivesse fazendo uma nova requisição, imagine dando f5 em uma transação bancária? ai complica né…

Mas tente fazer a seguinte alteração:

adiciona:

<p:commandButton action="#{vendasBeans.adiciona}" value="Ok"  style="position: absolute; left:215px">
    <f:ajax execute="@form" render="@form :formCliente" />
</p:comandButton>

Dê um id pro seu form de listagem = “formCliente” testa e ve se rola…

vlw

Iae galera muito Obrigado pelas respostas!!

eu achei uma solução aki


public String adiciona() throws SQLException, IOException {
		
		vendasDao dao = new vendasDao();
		dao.inserir(cliente);
		cliente = null;
		
		//FacesContext.getCurrentInstance().getExternalContext().redirect("/Projeto_vendas/faces/lista.xhtml");
		//return listando();
		return "/lista.xhtml?faces-redirect=true";
	}

fica a resposta aii pra quem estiver com o mesmo problema!

Abraços!