+ -
当前位置:首页 → 问答吧 → 请教解决方案

请教解决方案

时间:2010-08-03

来源:互联网

本帖最后由 末代地主 于 2010-08-03 16:09 编辑

需求是这样的.

有一个对象
class A{
   private Integer id;
   private String name;
   
   //....get set
}

在对象B
class B{
    private List<A> list;
   
    //....get set
}

然后测试类Test
class Test{
      public static void main(String[] a){
            Map map=new HashMap();
            map.put("list.id",new Long[]{1,2,3});//动态变化的
        map.put("list.name",new String[]{"test1","test2","test3"});//动态变化的

     }
}

现在需要动态的将A对象列表注入到B对象的list属性中

希望得到list中是
第一个对象id=1,name=test
第二个id=2,name=test2那样的效果

类似页面的动态生成表单项后提交一样

我用beanutils没有能实现,请教一下各位,谢谢了!

作者: 末代地主   发布时间: 2010-08-03

不懂 ?? 你是想自动注入??
还是想怎么样?

作者: tong0245   发布时间: 2010-08-04

是的,就是想将参数动态注入
测试类完整应该是这样

然后测试类Test
class Test{
      public static void main(String[] a){
            Map map=new HashMap();
            map.put("list.id",new Long[]{1,2,3});//动态变化的
        map.put("list.name",new String[]{"test1","test2","test3"});//动态变化的
        
        B b=new B();
            BeanUtils.populate(b,map);//这种方式注入普通属性没问题,但这种动态list里面的对象不行

     }
}

作者: 末代地主   发布时间: 2010-08-04

回复 末代地主


    如果是自己new  干脆自己set
如果不是我帮不上了。beanutil 几年前在web环境用过。没传过list

作者: tong0245   发布时间: 2010-08-04

相关阅读 更多