0

In my spring aplication, I am trying pass a array of strings from my view, through this code:

$( ".form" ).submit(function( event ) {
      event.preventDefault();
      var $form = $( this ), url = $form.attr( "action" );

      var selecao_permissoes=[];
        $('#selecao option').each(function(){
            selecao_permissoes.push($(this).val());
        });

      var nome = $("input[name=nome]").val();

      $.ajax({
          type: "POST",
          url: url,
          data: { nome: nome, permissoes: selecao_permissoes }
        }).done(function( msg ) {
            $("#"+msg).show();
            $(".form").each (function(){
                  this.reset();
            });
        });
});

To this method from my Service class:

public boolean cadastra(HttpServletRequest request, HttpServletResponse response) {
    String nome_grupo = request.getParameter("nome");
    String[] permissoes = request.getParameterValues("permissoes");

    if(nome_grupo == null || permissoes == null) {
        System.out.println("nome_grupo = "+nome_grupo);
        System.out.println("permissoes = "+permissoes);
        return false;
    }

    GrupoPermissao grupo = new GrupoPermissao();
    grupo.setNome(nome_grupo);

    List<Permissao> lista = new ArrayList<Permissao>();
    for(int i=0; i<permissoes.length; i++)
        lista.add(permissao.findById(Integer.valueOf(permissoes[i]).intValue()));
    grupo.setPermissao(lista);

    return grupo_permissao.persist(grupo);
}

The atribute 'nome' is receiving the correct value, but the atribute 'permissoes' is receiving a null value.

Anyone can tell me why this is happening? I can't figure out a motive for that.

2
  • Have you ever seen a complex javascript datatype in a URL? Commented May 22, 2014 at 20:46
  • This has nothing to do with JSP. You're making a client-side request using jQuery. Commented May 22, 2014 at 20:53

1 Answer 1

1

Try with:

String[] permissoes = request.getParameterValues("permissoes[]");

I don't know why, it's just annoying and how jquery are doing if for some reason if your posting an array and want it in your java servlet.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.