【问题标题】:How to pass a Javascript array to the Spring Controller Class如何将 Javascript 数组传递给 Spring 控制器类
【发布时间】:2013-08-13 18:32:15
【问题描述】:

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

【问题讨论】:

  • JavaScript 代码: var deleteWidgetId = new Array(); //创建数组 deleteWidgetId[0] = "a";//添加值 deleteWidgetId[0] = "b"; //动作触发 $("#saveLayout").load("layout/saveLayout.action", { deleteWidgetId : deleteWidgetId }, function(response, status, xhr) { });
  • Java 代码:(在控制器类中) @RequestMapping(value = "/saveLayout") public ModelAndView saveLayout(@RequestParam String[] deleteWidgetId) throws Exception { //这里如果我尝试使用deleteWidgetId它会给出空指针异常}

标签: java javascript spring spring-mvc


【解决方案1】:

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

【讨论】:

  • 我尝试使用 List 但它仍然显示该 deleteWidgetId 的 Null
  • 尝试将deleteWidgetId js变量重命名为deleteWidgetIdValues
【解决方案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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-25
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 2021-09-24
    • 2019-08-12
    • 1970-01-01
    相关资源
    最近更新 更多