Refresh page inserts again

Asked

Viewed 67 times

1

I’m starting web development with java and I’m doing some tests and I came across the following situation, I created a form that loads a list based on every click of the button, however, when I refresh the page I still load the list with the last object that was inserted in the list. Do you understand? If so, why does this happen?

EDIT Codes.

Method that performs student insertion in a list.

    public void incluirAluno(){
    this.listaAlunos.add(aluno);
    System.out.println("Aluno: "+aluno.getNome()+" foi adicionado com sucesso!");           
    this.aluno = new Aluno();
}

View

<h:form>
        <p:fieldset legend="Formulário de Cadastro de Clientes" toggleable="true" toggleSpeed="500">

        <b>Nome Completo    </b><br/>   
        <p:inputText value="#{controllerAluno.aluno.nome}" placeholder="Digite seu Nome Aqui" style="width:30%"/>                        
        <p></p>             
        <b>Data de Nascimento</b>   <br/>                            
        <p:calendar id="effect" value="#{controllerAluno.aluno.data_Nascimento}" effect="fold" placeholder="10/10/2010"/> 
        <p></p>             
        <b>Sexo</b><br/>
        <p:selectOneMenu id="console"   value="#{controllerAluno.aluno.sexo}"  style="width:125px">        
        <f:selectItem itemLabel="Masculino" itemValue="Masculino" />
        <f:selectItem itemLabel="Feminino" itemValue="Feminino" />
        <f:selectItem itemLabel="Indiferente" itemValue="Indiferente" />
        </p:selectOneMenu>
        <p></p>          
        <h:commandButton value="Incluir" action="#{controllerAluno.incluirAluno}"/>             
        <p></p>                                                                            
        </p:fieldset>
    </h:form>       
  • Add code snippets containing the methods involved, as well as your view.

1 answer

5

This is because you are submitting the same inclusion request when updating the page. This is a security breach that must be addressed in the architecture of your system. One way to treat this is to add a piece of protection against F5 that invalidates the status of a submitted request in an invalid stream. In your case you just need to check if the same student is being inserted again and in this case block the insertion. An example of how this can be done is:

public void incluirAluno(){
 if (isAlunoJaAdicionado(aluno) {
    addMensagemErro("O aluno selecionado já foi adicionado!");
    return;
 }
 this.listaAlunos.add(aluno);
 System.out.println("Aluno: "+aluno.getNome()+" foi adicionado com sucesso!");           
 this.aluno = new Aluno();
}

public boolean isAlunoJaAdicionado(Aluno aluno) {
  return this.listaAlunos.contains(aluno);
}

In order for the method contains to identify that the student has already been added, you need to implement the equals method in your student class: Example:

@Override
public boolean equals(Object obj) {
  return this.nome.equals(((Aluno) obj).getNome());
}

NOTE: addMensagemErro would be implemented according to the messaging engine you are using. Example with JSF:

public void addMensagemErro(String mensagem) {
    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, mensagem, null);
    FacesContext.getCurrentInstance().addMessage(null, message);
}

Browser other questions tagged

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