Refresh page re-inserts

1

I'm starting web development with java and I'm doing some testing 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. Could you understand? If so, why does this happen?

EDIT Codes.

Method that performs student insertion into 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>       
    
asked by anonymous 26.08.2016 / 18:56

1 answer

5

This is because you are submitting the same include request when you refresh the page. This is a security flaw that must be addressed in the architecture of your system. One way to handle this is to add an excerpt from F5 protection that invalidates the state of a submitted request in an invalid flow. In your case you would only need to verify that 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);
}

For the contains method 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: The addErrorMessage 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);
}
    
28.08.2016 / 16:51