如何将Javascript数组传递给Spring Controller类

时间:2013-08-12 12:33:52

标签: java javascript spring spring-mvc

我创建了JS数组并尝试将数组传递给Controller类,但它显示的是NullPointerException

我通过FireBug检查了URL,其中值正在传递,但是在控制器类中,如果我尝试检索它,则显示为NULL。

JavaScript代码:

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代码(在控制器类中):

@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
}

2 个答案:

答案 0 :(得分:1)

尝试使用List<String>代替String[]

答案 1 :(得分:1)

您必须在注释中指定请求参数的名称:

@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
}