Spring 3 中的字符串数组问题

Issue with String array in Spring 3

本文关键字:数组 问题 字符串 Spring      更新时间:2023-09-26

我正在尝试将 String[] 从客户端发布到 Spring 3。在控制器端,我定义了这样的方法。

@RequestMapping(value = "somemethod", method = RequestMethod.POST)
            public ModelAndView exportSomething(@RequestParam("sentences") String[] sentences) {
                 //.. logic
}

im 发送的数据如下所示

sentences: ["a","b,c","d"] 

问题在于在服务器端,句子数组的大小为 4。它将b和c拆分为两个不同的单词。

这是 Spring 的问题还是我需要更改传递数据的方式?

我想这是

Spring框架的一个已知问题。见 https://jira.springsource.org/browse/SPR-7963

尝试以这种格式发送数据。

句子:"a;b,c;d"请注意,在这种情况下,您的分隔符是 ;不是,所以你发送了一个字符串,其中包含一个列表

@RequestMapping(value = "somemethod", method = RequestMethod.POST)
        public ModelAndView exportSomething(@RequestParam("sentences") String sentences) {
            String[] sentenceArray = sentences.split(";");
            for(String tempString:sentenceArray){
               // perform what operation you want to perform
            }

}

在这种情况下,您将获得一个大小为三而不是四的数组。

您的方法不起作用的原因可能是因为您使用了逗号,这是数组的默认分隔符。