Olá pessoal tenh uma aplicação em JSF onde faço listagem ediçao e exclusão de noticias, até ai tudo bem, esta funcionando, quando adiciono uma noticia na tela de adição o programa redireciona para a listagem de noticias, porém quando clico F5 ele adiciona 2 vezes o mesmo objeto que havia acabado de adicionar. Tentei limpar o objeto apos a adiçao e listagem mas mesmo assim continuo com o problema. Segue abaixo o código para analise, qualquer ajuda é bem vinda.
Listagem de noticias
<h:form id="formNovaNoticia">
<div class="opcoes">
<h1>Nova NotÃcia</h1>
</div>
<h:messages errorStyle="alerta1" infoStyle="alerta2" warnStyle="alerta3" fatalStyle="alerta4" />
<h:panelGrid id="panelNovaNoticia" columns="2" styleClass="fieldset">
<h:outputLabel value="TÃtulo:"/>
<h:inputText value="#{NoticiaMB.noticia.titulo}" required="true" requiredMessage="'TÃtulo' não preenchido." styleClass="inputTitulo" />
<h:outputLabel value="Resumo:"/>
<h:inputTextarea value="#{NoticiaMB.noticia.resumo}" cols="70" rows="3" required="true" requiredMessage="'Resumo' não preenchido."/>
<h:outputLabel value="Descrição:"/>
<h:inputTextarea value="#{NoticiaMB.noticia.descricao}" cols="70" rows="15" required="true" requiredMessage="'Descrição' não preenchida."/>
<h:outputLabel value="Ativa"/>
<h:panelGrid columns="2" styleClass="panelButton">
<h:commandButton styleClass="button" value="Cadastrar" action="#{NoticiaMB.adicionaNoticia}"/>
<h:commandButton styleClass="button" value="Cancelar" action="listarNoticias" immediate="true"/>
</h:panelGrid>
</h:form>
Adição de noticia:
<h:form id="formListarNoticias">
<div class="opcoes">
<h:panelGrid rendered="#{AutenticaMB.usuario.nivel == 0}">
<h:commandButton styleClass="button" value="Nova notÃcia" action="novaNoticia"/>
</h:panelGrid>
</div>
<h1>Relação de NotÃcias</h1>
<h:messages errorStyle="alerta1" infoStyle="alerta2" warnStyle="alerta3" fatalStyle="alerta4" />
<h:panelGrid id="panelListarNoticias">
<h:dataTable id="tableNoticias" value="#{NoticiaMB.listarNoticias}" var="noticia" styleClass="listagem" rowClasses="linha1, linha2" columnClasses="alignCenter" >
<h:column>
<f:facet name="header">
<h:outputText value="ID"/>
</f:facet>
<h:outputText value="#{noticia.pkNoticia}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="TÃtulo"/>
</f:facet>
<h:outputText value="#{noticia.titulo}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Autor"/>
</f:facet>
<h:outputText value="#{noticia.fkUsuario}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Inclusão"/>
</f:facet>
<h:outputText value="#{noticia.dataInclusao}">
<f:convertDateTime pattern="dd/MM/yyyy "/>
</h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Atualização"/>
</f:facet>
<h:outputText value="#{noticia.dataAtualizacao}">
<f:convertDateTime pattern="dd/MM/yyyy "/>
</h:outputText>
</h:column>
<h:column rendered="#{AutenticaMB.usuario.nivel == 0}">
<f:facet name="header">
<h:outputText value="Ações"/>
</f:facet>
<a4j:commandLink ajaxSingle="true" action="editarNoticia">
<f:setPropertyActionListener target="#{NoticiaMB.noticia}" value="#{noticia}"/>
<h:graphicImage value="/images/editar.gif"/>
</a4j:commandLink>
<a4j:commandLink ajaxSingle="true" reRender="panelListaNoticias" action="#{NoticiaMB.excluirNoticia}" onclick="if (!confirm('Deseja realmente excluir a noticia?')) return false">
<f:setPropertyActionListener target="#{NoticiaMB.noticia.pkNoticia}" value="#{noticia.pkNoticia}"/>
<h:graphicImage value="/images/excluir.gif"/>
</a4j:commandLink>
</h:column>
</h:dataTable>
</h:panelGrid>
</h:form>
NoticiaControle:
//Imports
public class NoticiaControle extends Controle {
private Noticia noticia;
private NoticiaDAO dao;
private Usuario usuarioLogado = (Usuario) WebContext.getSessionAttribute("usuario");
public NoticiaControle() {
noticia = new Noticia();
dao = new NoticiaDAO();
setDao(dao);
}
//Getters and Setters
/**
* Lista todos as Empresas do banco de dados
*
* @return lista
*/
public List<Noticia> getListarNoticias() throws NamingException {
List lista = null;
try {
abrirConexao();
lista = dao.listar();
fecharConexao();
limparNoticia();
} catch (SQLException ex) {
logg.error("Erro ao tentar listar Noticias: " + ex);
}
return lista;
}
/**
* Adiciona uma Noticia no banco de dados
* @return
* @throws NamingException
*/
public String adicionaNoticia() throws NamingException, SQLException {
boolean retorno = false;
noticia.setFkUsuario(usuarioLogado.getPkMatricula());
abrirConexao();
retorno = dao.adicionar(noticia);
fecharConexao();
if (retorno) {
this.addMessage("noticia", INF_ADICIONAR_NOTICIA, FacesMessage.SEVERITY_INFO, "");
limparNoticia();
} else {
this.addMessage("noticia", ERR_ADICIONAR_NOTICIA, FacesMessage.SEVERITY_ERROR, "");
}
return "listarNoticias";
}
/**
* Edita uma Noticia no banco de dados
*
* @retono mensagem
*/
public String editarNoticia() throws NamingException {
boolean retorno = false;
noticia.setFkUsuario(usuarioLogado.getPkMatricula());
try {
abrirConexao();
retorno = dao.editar(noticia);
fecharConexao();
limparNoticia();
} catch (SQLException ex) {
logg.error("Erro ao tentar editar Usuario: " + ex);
}
if (retorno) {
this.addMessage("noticia", INF_ALTERAR_NOTICIA, FacesMessage.SEVERITY_INFO, "");
limparNoticia();
} else {
this.addMessage("noticia", ERR_ALTERAR_NOTICIA, FacesMessage.SEVERITY_ERROR, "");
}
return "listarNoticias";
}
/**
* Exclui uma Noticia do banco de dados
* @throws NamingException
*/
public void excluirNoticia() throws NamingException {
boolean retorno = false;
try {
abrirConexao();
retorno = dao.excluir(noticia);
fecharConexao();
if (retorno) {
this.addMessage("noticia", INF_EXCLUIR_NOTICIA, FacesMessage.SEVERITY_INFO, "");
limparNoticia();
} else {
this.addMessage("noticia", ERR_EXCLUIR_NOTICIA, FacesMessage.SEVERITY_ERROR, "");
}
} catch (SQLException ex) {
logg.error("Erro ao tentar excluir Noticia: " + ex);
}
}
}
Faces-config.xhtml
<navigation-case>
<from-outcome>listarNoticias</from-outcome>
<to-view-id>/noticia/listarNoticias.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>novaNoticia</from-outcome>
<to-view-id>/noticia/novaNoticia.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<managed-bean>
<managed-bean-name>NoticiaMB</managed-bean-name>
<managed-bean-class>controle.NoticiaControle</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>