问题描述
考虑以下控制器方法:
@RequestMapping(value = "/test", method = RequestMethod.GET)
public void test(@RequestParam(value = "fq", required = false) String[] filterQuery) {
logger.debug(fq = " + StringUtils.join(filterQuery, "|"));
}
以下是不同 fq
组合:
-
/ test?fq = foo
code> fq = foo -
/ test?fq = foo& fq = bar
在fq = foo | bar
-
/ test?fq = foo,bar
结果fq = foo | bar
-
/ test?fq = foo,bar& fq = bash
结果fq = foo,bar | bash
-
/ test?fq = foo,bar& fq =
结果fq = foo,bar |
/test?fq=foo
results infq = foo
/test?fq=foo&fq=bar
results infq = foo|bar
/test?fq=foo,bar
results infq = foo|bar
/test?fq=foo,bar&fq=bash
results infq = foo,bar|bash
/test?fq=foo,bar&fq=
results infq = foo,bar|
示例3是问题,我希望(想/需要)输出 fq = foo,bar
。
Example 3 is the problem. I expect (want/need) it to output fq = foo,bar
.
我尝试过es使用 \
并使用%3C
缩小逗号,但工作更加简单。
I've tried escaping the comma with \
and using %3C
but niether work.
如果我看看 HttpServletRequest
对象的版本:
If I look at the HttpServletRequest
object's version:
String[] fqs = request.getParameterValues("fq");
logger.debug(fqs = " + StringUtils.join(fqs, "|"));
它打印预期的输出: fqs = foo,bar
所以问题是与Spring数据绑定。
It prints the expected output: fqs = foo,bar
. So the "problem" is with the Spring data binding.
我可以绕过Spring的绑定并使用 HttpServletRequest
,但我真的不想因为我使用支持bean 在我的真实代码(同样的事情发生),并且不希望重新实现绑定功能。我希望有人可以提供一种简单的方式通过转义或其他机制来防止这种行为。
I could by-pass Spring's binding and use HttpServletRequest
but I really don't want to as I'm using a backing bean in my real code (same thing is happening) and don't wish to re-implement the binding functionality. I'm hoping someone can provide a simple way of preventing this behavior via escaping or some other mechanism.
TIA
更新:我在Twitter上发布了这个问题,并得到了一个回复,说预期的输出与Spring 3.0.4.RELEASE 一起出现,现在我已经证实了这种情况,因此是一个临时修复,我将继续将其作为Spring JIRA系统的一个错误记录,如果有的话可以提供一个工作或修复3.0.5,我会接受他们的答案。
UPDATE: I posted this Q on Twitter and got a reply saying the expected output appears with Spring 3.0.4.RELEASE. I've now confirmed this is the case and thus is a temporary fix. I'll go ahead and log this as a bug on the Spring JIRA system. If anyone can provide a work around or fix with 3.0.5, I'll accept their answer.
推荐答案
我已经测试了你的代码:这是令人难以置信的,但我无法重现你的问题。我已经下载了最新版本的spring(3.0.5),这是我的控制器:
I've tested your code: it's unbelievable, but I can't reproduce your issue. I've downloaded the latest version of spring (3.0.5), this is my controller:
package test;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/test/**")
public class MyController {
private static final Logger logger = Logger.getLogger(MyController.class);
@RequestMapping(value = "/test/params", method = RequestMethod.GET)
public void test(SearchRequestParams requestParams, BindingResult result) {
logger.debug("fq = " + StringUtils.join(requestParams.getFq(), "|"));
}
}
这是我的SearchRequestParams类:
this is my SearchRequestParams class:
package test;
public class SearchRequestParams {
private String[] fq;
public String[] getFq() {
return fq;
}
public void setFq(String[] fq) {
this.fq = fq;
}
}
这是我简单的弹簧配置:
and this is my simple spring configuration:
<bean id="urlMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="test.MyController" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
我在tomcat 7.0.8中测试了我的代码;当我键入 http:// localhost:8080 / testweb / test / params.htm?fq = foo,bar
我可以在我的日志文件中读这行: DEBUG fq = foo,bar
。
与我的代码有什么不同?我做错了吗?
我想帮你,所以如果你有任何疑问,或者我可以为你做一些其他的测试,这将是一种荣幸。
I've tested my code within tomcat 7.0.8; when I type http://localhost:8080/testweb/test/params.htm?fq=foo,bar
I'm able to read in my log file this line: DEBUG fq = foo,bar
.What are the the differences from my code to yours? Am I doing something wrong?I'd like to help you, so if you have any doubts or if I can do some other tests for you, it will be a pleasure.
更新/解决方案
随着您的代码我已经重现了这个问题;您的dispatcher servlet配置中包含标签< mvc:annotation-driven />
,因此您将默认使用默认转换服务, FormattingConversionService
,其中包含从 String
到 String []
的默认转换器,它使用逗号作为分隔器。
您必须使用包含您自己的转换器的其他转换服务bean从 String
到 String []
。你应该使用一个不同的分隔符,我选择使用;因为它是查询字符串中常用的分隔符(?first = 1; second = 2; third = 3):
UPDATE / SOLUTION
With your code I've reproduced the issue; you have the tag <mvc:annotation-driven />
in your dispatcher servlet configuration, so you silently use a default conversion service, instance of FormattingConversionService
, which contains a default converter from String
to String[]
that uses comma as separator.You have to use a different conversion service bean containing your own converter from String
to String[]
. You should use a different separator, I've choosed to use ";" because it's the separator commonly used into query string ("?first=1;second=2;third=3"):
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;
public class CustomStringToArrayConverter implements Converter<String, String[]>{
@Override
public String[] convert(String source) {
return StringUtils.delimitedListToStringArray(source, ";");
}
}
然后你必须在你的配置:
Then you have to specify this conversion service bean in your configuration:
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="au.org.ala.testspringbinding.CustomStringToArrayConverter" />
</list>
</property>
</bean>
问题已经修复,现在您应该检查是否有任何副作用。我希望您在应用程序中不需要从 String
到 String []
(以逗号分隔符)的原始转换)。 ;-)
The issue has fixed, now you should check for any side effects. I hope you don't need in your application the original conversion from String
to String[]
(with comma as separator). ;-)
这篇关于如何防止参数绑定在Spring 3.0.5中解释逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!