【问题标题】:Issue with String array in Spring 3Spring 3 中的字符串数组问题
【发布时间】:2012-06-07 11:21:58
【问题描述】:

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

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

我发送的数据是这样的

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

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

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

【问题讨论】:

    标签: java javascript spring spring-3


    【解决方案1】:

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

    【讨论】:

    • 如果有帮助,请采纳答案
    【解决方案2】:

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

    句子:“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
                }
    

    }

    在这种情况下,您将得到一个大小为 3 而不是 4 的数组。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-07
      • 1970-01-01
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多