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

How to pass a Javascript array to the Spring Controller Class

本文关键字:Spring Controller Javascript 数组      更新时间:2023-09-26

我创建了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
}

尝试使用List<String>而不是String[]

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

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