我有一个带有许多其他嵌套对象和对象列表的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不会出现问题。但是未填充defListabc.ghi。这是Struts 2的常见行为吗?有什么方法可以覆盖它吗?

最佳答案

问题解决了。支柱2的岩石。由于我得到的代码是针对错误修复的,因此不知道其中包含什么,甚至没有检查过一次。

罪魁祸首是被覆盖的toString()方法。它没有在地图上检查是否为null,并在其上调用了entrySet()方法。生成异常并阻止Struts填充对象。

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

07-24 09:49
查看更多