I have made a tag input section on a page and want to save the tags as a user enters them.
So I am trying to pass a Javascript array of Strings to a spring controller.
This is javascript to populate the tagList array. It basically gives me back an array as so:
var taglist = [];
a[0] = hi;
a[1] = bye;
a[2] = shoe;
This is the way I try to do it.
$(':submit').click(function() {
alert("array is "
+ JSON.stringify(tagList));
alert("enter ajax");
$.ajax({
type : "POST",
url : "/json/tags",
data : {
tagList : a
},
success : function(response) {
alert("good results")
},
error : function(e) {
alert('Error: ' + e);
}
});
})
The list is being populated fine, it is just when I try to pass this List to my controller.
@RequestMapping(value = "/json/tags", method = RequestMethod.POST)
public String controllerMethod(@RequestParam(value="tagList[]") String[] myArray){
System.out.println(myArray);
if(myArray.length> 2){
return "good";
}else{
return "bad";
}
}
I was following this tutorial which does it with an Integer array, but can't seem to it to work with the String array.