Spring @RequestParam 无法正确处理多个变量 - 示例测试用例
下面给出的测试用例显示了一个简单的情况,其中我有 2 个参数 paramA
和 paramB
。
- 如果我调用
/paramtest
url,则会调用paramtest()
方法。 - 如果我为
paramA
输入true
,则会调用方法aTrue()
。 - 但是,当我为
paramA
和paramB
输入true
时,将调用方法bTrueNotA()
。
但第三个 @RequestMapping
需要 A=True
和 B!=true
。根据我的侦察,当两个参数都为 true 时,应该调用 aTrue()
。
@RequestMapping("paramtest")
@ResponseBody
public String paramtest(){
return "<html><head></head><body>" +
"<form action=paramtest method=post>" +
"paramA: <input type=text name=paramA /><br>" +
"paramB: <input type=text name=paramB /><br>" +
"<input type=submit>" +
"</form>" +
"</body></html>";
}
@RequestMapping(value="paramtest", params="paramA=true")
@ResponseBody
public String aTrue(){
return "A=true";
}
@RequestMapping(value="paramtest", params={"paramB=true", "paramA!=true"})
@ResponseBody
public String bTrueNotA(){
return "B=True; A!=true";
}
The test case given below shows a simple case where I have 2 parameters paramA
and paramB
.
- If I call the
/paramtest
url theparamtest()
method is called. - If I enter
true
forparamA
, methodaTrue()
is called. - However, when I enter
true
for bothparamA
andparamB
the methodbTrueNotA()
is called.
But the 3rd @RequestMapping
calls for A=True
and B!=true
. By my reconing when both parameters are true, aTrue()
should be called.
@RequestMapping("paramtest")
@ResponseBody
public String paramtest(){
return "<html><head></head><body>" +
"<form action=paramtest method=post>" +
"paramA: <input type=text name=paramA /><br>" +
"paramB: <input type=text name=paramB /><br>" +
"<input type=submit>" +
"</form>" +
"</body></html>";
}
@RequestMapping(value="paramtest", params="paramA=true")
@ResponseBody
public String aTrue(){
return "A=true";
}
@RequestMapping(value="paramtest", params={"paramB=true", "paramA!=true"})
@ResponseBody
public String bTrueNotA(){
return "B=True; A!=true";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这可能是 Spring 的一个错误。我尝试使用以下映射:
并使用带有以下参数的现有表单,这些是我得到的结果:
paramA=true
A() 按预期调用paramA=true, paramB=foobar
B() 按预期调用paramA=not_true, paramB=foo
404 页面,而不是按预期调用 C()。我在 Tomcat 控制台上收到此错误:
All of this with Spring 3.0.5。请注意,
myParam!=myValue
仅从 Spring 3.0.4 开始可用(3.0.3 doc 未列出该选项)。另外,我认为!myParam=myValue
无效,因为它没有在 当前 3.0.5 文档。抱歉,这不能解决您的问题,但想分享我的调查:)
I think it might be a bug in Spring. I tried with the following mappings:
and using your existing form with the following parameters, these were the results I got:
paramA=true
A() called as expectedparamA=true, paramB=foobar
B() called as expectedparamA=not_true, paramB=foo
404 page, and not C() as expected.I got this error on the Tomcat console:
All of this with Spring 3.0.5. Note that the
myParam!=myValue
was only available from Spring 3.0.4 onwards (the 3.0.3 doc does not list that option). Also, I do not think that!myParam=myValue
is valid, as this is not listed in the current 3.0.5 documentation.Sorry this is not a solution to your problem but wanted to share my investigation :)