【问题标题】:How to get Post parameters in Spring MVC controller?如何在 Spring MVC 控制器中获取 Post 参数?
【发布时间】:2017-12-04 12:47:05
【问题描述】:

我试图在 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

【问题讨论】:

    标签: jquery ajax spring spring-mvc


    【解决方案1】:

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

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

    或者

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

    【讨论】:

    • 还是找不到参数。
    • 对不起,我更新了答案,我相信这次它会起作用
    • 我将 $.post 方法更新为 $.ajax 方法,现在可以正常工作了,谢谢。
    • 有一个更好的解决方案,你可以添加一个参数String[] types来更新方法,spring mvc会智能地为你注入参数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 2011-11-10
    • 1970-01-01
    • 2016-07-29
    • 2014-07-02
    相关资源
    最近更新 更多