You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by James Jithin <ja...@gmail.com> on 2011/08/28 07:45:59 UTC

Struts2 POJO population from URL

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?

- Jim

Re: Struts2 POJO population from URL

Posted by Maurizio Cucchiara <mc...@apache.org>.
did you take a look at log? did you see any error?
I don't know if it really help, but I usually prefer to initialize the
collection inside the action class directly.
For instance:
private List<Def> defList=new ArrayList<Def>(20);

Such that you gain more control over the chosen implementation, the lenght, etc.

Maurizio Cucchiara



On 28 August 2011 07:45, James Jithin <ja...@gmail.com> wrote:
> 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?
>
> - Jim
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts2 POJO population from URL

Posted by Maurizio Cucchiara <ma...@gmail.com>.
In the same way, try to initialize the Ghi and Def object before the
action is invoked.
Maurizio Cucchiara



On 28 August 2011 07:45, James Jithin <ja...@gmail.com> wrote:
> 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?
>
> - Jim
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org