mohammed abuthaheer
mohammed abuthaheer

Reputation: 47

How to pass a Javascript array to the Spring Controller Class

I created the JS array and try to pass the array to the Controller class, but there it is showing the NullPointerException.

I checked the URL through FireBug there the values are passing but in controller class if I try to retrive it is showing NULL.

JavaScript Code:

var deleteWidgetId = new Array(); //array created 
deleteWidgetId[0] = "a";//adding values 
deleteWidgetId[1] = "b"; 
//action trigged 
$("#saveLayout").load("layout/saveLayout.action", 
 { deleteWidgetId : deleteWidgetId },      
 function(response, status, xhr) { });

Java Code (In controller class):

@RequestMapping(value = "/saveLayout")
public ModelAndView saveLayout(@RequestParam String[] deleteWidgetId) throws Exception { 
    //here that if i try to use the deleteWidgetId it is giving null pointer exception
}

Upvotes: 2

Views: 5404

Answers (2)

Amin Abu-Taleb
Amin Abu-Taleb

Reputation: 4511

You have to specify the name of the request parameter within the annotation:

@RequestMapping(value = "/saveLayout")
public ModelAndView saveLayout(@RequestParam(value="deleteWidgetId") String[] deleteWidgetId) throws Exception { 
    //here that if i try to use the deleteWidgetId it is giving null pointer exception
}

Upvotes: 1

yname
yname

Reputation: 2245

Try to use List<String> instead of String[]

Upvotes: 1

Related Questions