java - 尝试将数组发送到 spring mvc Controller 时出现 "Bad Request"

标签 java jquery spring spring-mvc

我看到了相关问题并尝试了那些都没有帮助的问题。我正在使用 jquery 发送 POST 请求,如下所示:

var data = {};          
            //this works every time and it's not issue
            var statusArray = $("#status").val().split(',');  
            var testvalue = $("#test").val();

                     data.test = testvalue;
            data.status = statusArray ;

             $.post("<c:url value="${webappRoot}/save" />", data, function() {
        })

在 Controller 方面,我尝试了以下操作:

public void saveStatus(ModelMap model, Principal principal, HttpSession session, final HttpServletResponse response, @RequestParam String test, @RequestBody String [] status) {

        //I never get to this point, but when I set statusArray to required false test variable is being populated correctly
        }


public void saveStatus(ModelMap model, Principal principal, HttpSession session, final HttpServletResponse response, @RequestParam String test, @RequestParam String [] status) {

        //I never get to this point, but when I set statusArray to required false test variable is being populated correctly
        }



public void saveStatus(ModelMap model, Principal principal, HttpSession session, final HttpServletResponse response, @RequestParam String test, @RequestParam("status") String [] status) {

        //I never get to this point, but when I set statusArray to required false test variable is being populated correctly
        }


public void saveStatus(ModelMap model, Principal principal, HttpSession session, final HttpServletResponse response, @RequestParam String test, @RequestParam(name="status") String [] status) {

        //I never get to this point, but when I set statusArray to required false test variable is being populated correctly
        }

这些都不起作用,我想知道我做错了什么,无论我做什么,我都会得到错误的请求

最佳答案

你的状态参数应该是@RequestParam(value = "status[]") String[] status (Spring 3.1)。

关于java - 尝试将数组发送到 spring mvc Controller 时出现 "Bad Request",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12984176/

相关文章:

java - 与 mySQL 一起使用的自动 java 类

javascript - 如何划分表格以分页显示?表格数据用jsp动态填充

javascript - jQuery 验证引擎不适用于 Bootstrap Select 插件

JQuery, CSS - 创建评级系统

java - 在 servlet 中使用 JSP 文件中的数据

java - 24/7 全天候运行 Java 服务器应用程序以及所有那些著名的在线实时应用程序背后的 secret

javascript - 无法从浏览器的本地存储中删除项目

java - 我想将步骤 1 中的数据传递给步骤 2 的读取器

java - SpringJDBC模板的实体或DTO方法?

java - 驱动程序 :org. postgresql.Driver@3ed03652 为 URL 返回 null ... 在将 spring boot 部署到 Heroku 时