开发者

Struts 2 does not populate POJO from client

I have a Java Object with many other nested Objects and lists of Objects. When the request arrives from the client, I see that the Object is populated only to a few levels. Is there any configuration that sets this is Struts 2? Here is my example.

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;
    }
}

When I call the populate method and when send to the jsp, the iteration happens good with all the elements. But, when I try to update, i.e. when then form is submitted, the update() method is called, but the instance variable abc does not get populated completely.

I have seen the url which is passed and everything seems to be fine. Let me tell you what happens. The url will be something like (splitting with line break for easy understanding here),

&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

In this case, the defList inside abc and ghi.ghiName in abc are populated with no issues. But the defList of abc.ghi is not populated. Is this a common behavior of Struts 2? Is ther any means by which this can be overridden?


Got the issue solved. Struts 2 rock. Since the code I got was for a bug fix, did not know what was inside it, not even checked it once.

The culprit was the toString() method which was overridden. This had no check on the map for null and called the entrySet() method on it. This generated Exception and prevented Struts from populating the objects.

For better understanding, Struts do call the toString() method for some purpose while populating. If anyone faces this in future, do remember to check if you have overridden toString() and if everything is set inside it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜