带有地图的 BeanUtils

发布于 2024-08-02 10:02:50 字数 220 浏览 3 评论 0原文

如何将 BeanUtils setProperty 方法与 Maps 一起使用。

例如,这个方法: public void setAddress(String类型,Address地址); 可以使用以下方式设置: BeanUtils.setProperty(beanObject, "地址(家)", addressObject );

但是如果我要设置的对象是一个Map,可以吗?如何?

How can I use the BeanUtils setProperty method with Maps.

For example, this method:
public void setAddress(String type, Address address);
Can be setted using:
BeanUtils.setProperty(beanObject, "address(home)", addressObject );

But if the object I want to set is a Map, is it possible? how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆橘 2024-08-09 10:02:50

使用映射语法foo(bar)bar是映射foo的键):

public static class Bean{
    private Map<String, String> data = new HashMap<String, String>();
    public Map<String, String> getData(){
        return data;
    }
    public void setData(final Map<String, String> data){
        this.data = data;
    }
}

public static void main(final String[] args) throws Exception{
    final Bean bean = new Bean();
    // assign the foo key of the data property to the value 'bar'
    BeanUtils.setProperty(bean, "data(foo)", "bar");
    System.out.println(bean.data);
}

输出:

{foo=bar}

Use the Map syntax foo(bar) (bar is the key of the map foo):

public static class Bean{
    private Map<String, String> data = new HashMap<String, String>();
    public Map<String, String> getData(){
        return data;
    }
    public void setData(final Map<String, String> data){
        this.data = data;
    }
}

public static void main(final String[] args) throws Exception{
    final Bean bean = new Bean();
    // assign the foo key of the data property to the value 'bar'
    BeanUtils.setProperty(bean, "data(foo)", "bar");
    System.out.println(bean.data);
}

Output:

{foo=bar}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文