Spring MVC:复杂对象为GET @RequestParam [英] Spring MVC: Complex object as GET @RequestParam
问题描述
假设我有一个列出表中对象的页面,我需要放置一个表单来过滤表。过滤器作为Ajax GET发送到如下URL: http://foo.com/system/controller/action?page=1&prop1=x&prop2=y&prop3=z
Suppose i have a page that lists the objects on a table and i need to put a form to filter the table. The filter is sent as an Ajax GET to an URL like that: http://foo.com/system/controller/action?page=1&prop1=x&prop2=y&prop3=z
而是在我的控制器上有很多参数,如:
And instead of having lots of parameters on my Controller like:
@RequestMapping(value = "/action")
public @ResponseBody List<MyObject> myAction(
@RequestParam(value = "page", required = false) int page,
@RequestParam(value = "prop1", required = false) String prop1,
@RequestParam(value = "prop2", required = false) String prop2,
@RequestParam(value = "prop3", required = false) String prop3) { ... }
假设我的MyObject为:
And supposing i have MyObject as:
public class MyObject {
private String prop1;
private String prop2;
private String prop3;
//Getters and setters
...
}
我想做类似的事情:
@RequestMapping(value = "/action")
public @ResponseBody List<MyObject> myAction(
@RequestParam(value = "page", required = false) int page,
@RequestParam(value = "myObject", required = false) MyObject myObject,) { ... }
有可能吗?
我该怎么办?
Is it possible? How can i do that?
推荐答案
你绝对可以这样做,只需删除 @RequestParam
注释,Spring会将您的请求参数干净地绑定到您的类实例:
You can absolutely do that, just remove the @RequestParam
annotation, Spring will cleanly bind your request parameters to your class instance:
public @ResponseBody List<MyObject> myAction(
@RequestParam(value = "page", required = false) int page,
MyObject myObject)
这篇关于Spring MVC:复杂对象为GET @RequestParam的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!