如何在Spring MVC控制器中获取Post参数?

时间:2017-12-04 12:47:05

标签: jquery ajax spring spring-mvc

我试图在Spring MVC控制器中获取Post参数,但ServletRequestUtils getStringParameters方法找不到此参数并返回空数组。有没有办法用ServletRequestUtils获取数组参数?

我的控制器;

@ResponseBody
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String update(HttpServletRequest request, HttpServletResponse response){

    //I can get id parameter without problem.
    String id = ServletRequestUtils.getStringParameter(request, "id", null);

    String[] types = ServletRequestUtils.getStringParameters(request, "types");
}

我的ajax帖子;

$.post("update", {
   id: id,
   types: types
   ....
}

我的表单数据;

id=2a4e905d-8015-447c-9d52-896042367c8a&types%5B%5D=IP+Num&types%5B%5D=Ver+Tab

1 个答案:

答案 0 :(得分:3)

尝试添加传统:true到像这样的选项

 $.ajax({
         url:'update',
         type:'post',
         data:{
               id: id,      
               types:types,
              ....
              },
          traditional:true 
        })

或者

$.post("update",  $.param({
   id: id,      
   types:types,
   ....
  },true)

}