Spring MVC:复杂对象作为 GET @RequestParam [英] Spring MVC: Complex object as GET @RequestParam

查看:51
本文介绍了Spring MVC:复杂对象作为 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆