如何通过一个Javascript数组到春节控制器类(How to pass a Javascript

2019-10-18 11:53发布

我建立的JS阵列和尝试通过阵列到控制器类中,但也有它表示NullPointerException

我通过萤火检查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
}

Answer 1:

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



Answer 2:

你必须指定注解内的请求参数的名称:

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


文章来源: How to pass a Javascript array to the Spring Controller Class