Struts 2不会从客户端填充POJO

时间:2011-08-27 19:30:07

标签: java struts2 ognl

我有一个带有许多其他嵌套对象和对象列表的Java对象。当请求从客户端到达时,我看到对象仅填充到几个级别。是否有任何配置设置这是Struts 2?这是我的例子。

class MyActionClass extends ActionSupport {
    private Abc abc;
    public Abc getAbc() {
        return abc;
    }
    public void setAbc(Abc abc) {
        this.abc = abc;
    }
    public String populate() {
        MyService myService = new MyService();
        abc = myService.getMyAbc();
        return SUCCESS;
    }
    public String update() {
        MyService myService = new MyService();
        myService.updateAbc(abc);
        return SUCCESS;
    }
}

class Abc {
    private List<Def> defList;
    private Ghi ghi;
    public void setDefList(List<Def> defList) {
        this.defList = defList;
    }
    public List<Def> getDefList(){
        return defList;
    }
    public void setGhi(Ghi ghi) {
        this.ghi = ghi;
    }
    public Ghi getGhi() {
        return ghi;
    }
}

class Def {
    private String name;
    private long id;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
}

class Ghi {
    private List<Def> defList;
    private String ghiName;

    public void setDefList(List<Def> defList) {
        this.defList = defList;
    }
    public List<Def> getDefList() {
        return defList;
    }
    public void setGhiName(String ghiName) {
        this.ghiName = ghiName;
    }
    public String getGhiName() {
        return ghiName;
    }
}

当我调用populate方法并发送到jsp时,迭代对所有元素都很好。但是,当我尝试更新时,即当提交表单时,调用update()方法,但实例变量abc不会完全填充。

我已经看到了传递的网址,一切似乎都很好。让我告诉你会发生什么。网址将是这样的(用换行符拆分以便于理解),

&abc.defList[0].name=alex
&abc.defList[0].id=1
&abc.defList[1].name=bobby
&abc.defList[1].id=2
&abc.ghi.ghiName=GHINAME
&abc.ghi.defList[0].name=Jack
&abc.ghi.defList[0].id=1
&abc.ghi.defList[1].name=Jill
&abc.ghi.defList[1].id=2

在这种情况下,defList中的abcghi.ghiName中的abc填充没有任何问题。但defList的{​​{1}}未填充。这是Struts 2的常见行为吗?是否可以通过任何方式来覆盖它?

1 个答案:

答案 0 :(得分:1)

问题解决了。 Struts 2摇滚。因为我得到的代码是修复bug,所以不知道里面有什么,甚至没有检查过一次。

罪魁祸首是被覆盖的toString()方法。这没有在地图上检查null并在其上调用entrySet()方法。这会生成Exception并阻止Struts填充对象。

为了更好地理解,Struts在填充时会出于某种目的调用toString()方法。如果将来有人面对此问题,请务必检查是否已覆盖toString()以及是否已在其中设置了所有内容。

相关问题