Spring 3 MVC - 将带有前缀的请求参数映射到单个 bean
我有以下 GET 请求:
/api/search?filter.operation=Ping&filter.namespace=
请注意参数名称包含前缀(filter. 操作、<强>过滤器。命名空间)。
然后,我想使用以下 bean 来接收这些参数:
class MessageSearchFilter {
String operation;
String namespace;
...
}
并且处理程序方法具有以下签名:
public @ResponseBody String searchMessages(MessageSearchFilter filter, ...);
但这并不'不能工作,因为 Spring MVC 期望“操作”和“命名空间”属性的命名与此完全相同。如果我将请求更改为使用“操作”和“命名空间”(没有“过滤器”前缀),它就会起作用。
有什么方法可以告诉 Spring 期望参数以“filter”为前缀吗?
与此相关的一个附带问题是,如果我的方法签名包含多个属性名称冲突的表单对象,会发生什么情况?
I have the following GET request:
/api/search?filter.operation=Ping&filter.namespace=
Please note the parameter names include a prefix (filter. operation, filter. namespace).
Then, I have the following bean I want to use to receive those parameters:
class MessageSearchFilter {
String operation;
String namespace;
...
}
And the handler method has the following signature:
public @ResponseBody String searchMessages(MessageSearchFilter filter, ...);
However this doesn't work as Spring MVC expects 'operation' and 'namespace' attributes to named exactly like that. It works if I change my request to use 'operation' and 'namespace' (with no 'filter.' prefix).
Is there any way I can tell Spring to expect parameters to be prefixed with 'filter'?
A side question related to this is, what happens if I have a method signature with several form objects with colliding property names?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够使用
@RequestParam
映射传入的查询字符串参数,并且可以完全限定它们:您还会注意到,您现在应该能够为具有属性名称冲突的其他对象添加限定符。
You should be able to map incoming querystring parameters using
@RequestParam
, and you can fully qualify them:You will also notice that you should be able to now add qualifiers for other objects with property name collisions.
将方法添加到控制器中
应该可以完成这项工作。
Adding to your controller the method
should do the job.