2

我有一个带有许多其他嵌套对象和对象列表的 Java 对象。当请求从客户端到达时,我看到 Object 仅填充到几个级别。是否有任何配置设置这是 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 并未完全填充。

我已经看到了通过的 url,一切似乎都很好。让我告诉你会发生什么。url 将类似于(为了便于理解,这里用换行符分割),

&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内部abc和内部ghi.ghiNameabc没有问题。但是defListofabc.ghi没有填充。这是 Struts 2 的常见行为吗?有什么方法可以覆盖它吗?

4

1 回答 1

1

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

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

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

于 2011-08-30T13:31:32.697 回答