Reputation: 9065
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.
Upvotes: 0
Views: 604
Reputation: 86
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.
Upvotes: 1