简体   繁体   中英

Passing array of Strings from JSP page to Server

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM