1

我看到了相关的问题并尝试了那些没有帮助的问题。我正在使用 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() {
        })

在控制器方面,我尝试了以下操作:

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
        }

这些都不起作用我想知道我做错了什么,无论我做什么Bad request

4

3 回答 3

1

我也遇到了同样的问题Bad request。我通过执行以下代码解决了它。
您可以通过JSON.stringify(array)将数组转换为 json 字符串来将数组发布到控制器。我已使用push()
将多个对象推入数组中。

    var a = [];
    for(var i = 1; i<10; i++){
        var obj = new Object();
        obj.name = $("#firstName_"+i).val();
        obj.surname = $("#lastName_"+i).val();
        a.push(obj);
    }

    var myarray = JSON.stringify(a);
    $.post("/ems-web/saveCust/savecustomers",{myarray : myarray},function(e) {

    }, "json");

控制器:
您可以使用 jackson 来处理 json 字符串。
Jackson 是一个高性能 JSON 处理器 Java 库。

    @RequestMapping(value = "/savecustomers", method = RequestMethod.POST)
    public ServiceResponse<String> saveCustomers(ModelMap model, @RequestParam String myarray) {

        try{
            ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); 
            List<DtoToMAP> parsedCustomerList = objectMapper.readValue(myarray, new TypeReference<List<DtoToMAP>>() { });
            System.out.println(" parsedCustomerList :: " + parsedCustomerList);
        }catch (Exception e) {  
            System.out.println(e);
        }
    }

注意:确保您的 dto 应该包含与您使用数组对象发布的相同的变量名称。
就我而言,我的 dto 包含 firstName,lastName,因为我使用数组对象发布。

杰克逊依赖:

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.3</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.3</version>
    </dependency>
于 2013-11-16T11:19:59.517 回答
1

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

于 2012-10-20T00:51:45.987 回答
0

我认为您的问题可能是要将数组发送到您必须多次实际发送参数的东西。

在 GET 操作的情况下,例如: ?status=FOO&status=BAR

我不确定spring会自动将逗号分隔的字符串转换为数组。但是,您可以添加一个 PropertyEditor(请参阅 PropertyEditorSupport)以逗号分隔字符串。

@InitBinder
public void initBinder(WebDataBinder binder) {
   binder.registerCustomEditor(String[].class, new PropertyEditorSupport() {
        @Override
        public String getAsText() {
            String value[] = (String[]) getValue();
            if (value == null) {
                return "";
            }
            else {
                return StringUtils.join(value, ",");
            }
        }

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            if (text == null || text.trim().length() == 0) {
                setValue(null);
            }
            else {
                setValue(StrTokenizer.getCSVInstance(text).getTokenArray());
            }
        }

    });
}

请注意,我使用 commons-lang 来连接和拆分字符串,但您可以轻松地使用任何您想要的方式自己完成。

一旦你这样做了,任何时候你想要一个参数从一个字符串绑定到一个 String[],spring 会自动为你转换它。

于 2012-10-20T01:00:25.793 回答