小编典典

Spring MVC:复杂对象为GET @RequestParam

java

假设我有一个列出表中对象的页面,并且我需要放置一个表格来过滤表。过滤器以Ajax GET的形式发送到如下网址:http : //foo.com/system/controller/action?page=1&prop1= x& prop2=y&prop3=z

而不是像我的控制器上那样有很多参数:

@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为:

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,) { ... }

可能吗?我怎样才能做到这一点?


阅读 764

收藏
2020-03-16

共1个答案

小编典典

你绝对可以做到这一点,只需删除@RequestParam注释,Spring就会干净地将你的请求参数绑定到你的类实例:

public @ResponseBody List<MyObject> myAction(
    @RequestParam(value = "page", required = false) int page,
    MyObject myObject)
2020-03-16