You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Justin Robbins <ju...@gmail.com> on 2011/06/22 21:53:30 UTC

POJO's collection not populated when submitting form

Hi folks,

I have a POJO which has a field of type java.util.Set that is
collection of domain objects. When I submit a form to my Action, all
the POJO fields are populated with values from the form except the
field which is the collection. I have no idea why this collection
isn't getting populated.  Any advice on how to resolve this problem or
how to better troubleshoot it would be much appreciated.

I'm posting some code excerpts below but please not that I posted
larger code excerpts for my example in a separate post StackOverflow.
I hope that's not considered a faux pas for cross posting but it
didn't seem these emails are a great place to copy and paste a bunch
of code.

http://stackoverflow.com/questions/6443498/pojos-collection-not-populated-when-submitting-form

I also have a second related question.  My POJO's collection is a
java.util.Set.  Does it matter to Struts 2 (or complicate things) that
I'm using a Set and not a List?  Is it common to use a Set as a
collection field in a POJO used in Struts 2?  The reason I'm using a
Set is that I'm using Hibernate for ORM and according to the
documentation a Set is the most common collection used association
based mappings in Hibernate.

<s:textfield name="flashCard.links.url" key="label.flashcard.link" size="66" />

I've also tried:
<s:textfield name="flashCard.links[0].url" key="label.flashcard.link"
size="66" />

>From the Action class:
FlashCard flashCard = new FlashCard();

public FlashCard getFlashCard() {
	return this.flashCard;
}

public void setFlashCard(FlashCard flashCard) {
	this.flashCard = flashCard;
}

>From the POJO:
private Set<Link> links = new HashSet<Link>(0);

public Set<Link> getLinks() {
       return this.links;
}

public void setLinks(Set<Link> links) {
      this.links = links;
 }

-- 
Regards,
Justin Robbins

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


Re: POJO's collection not populated when submitting form

Posted by Dave Newton <da...@gmail.com>.
On Mon, Jun 27, 2011 at 10:57 AM, Justin Robbins
<ju...@gmail.com> wrote:
> I wonder why the Hibernate docs use Set and mention Set is the most
> commonly used Collection in mappings.

Maybe it is, I guess I've just never found it helpful in anything I've
done--I've usually needed either a known order, or to be able to get
specific items in the collection by ID. Dunno :)

I honestly don't recall how the default XW2 handles sets (the built-in
type conversion irritates me, actually, the way it's implemented) but
perhaps it's something worth looking in to and cleaning up.

Dave

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


Re: POJO's collection not populated when submitting form

Posted by Justin Robbins <ju...@gmail.com>.
Many thanks for the confirmation Dave.  I'll convert my Hibernate
mappings to List or Map.

I wonder why the Hibernate docs use Set and mention Set is the most
commonly used Collection in mappings.  I realize that is a discussion
for another venue though.  Thanks again.

On Wed, Jun 22, 2011 at 3:53 PM, Justin Robbins
<ju...@gmail.com> wrote:
> Hi folks,
>
> I have a POJO which has a field of type java.util.Set that is
> collection of domain objects. When I submit a form to my Action, all
> the POJO fields are populated with values from the form except the
> field which is the collection. I have no idea why this collection
> isn't getting populated.  Any advice on how to resolve this problem or
> how to better troubleshoot it would be much appreciated.
>
> I'm posting some code excerpts below but please not that I posted
> larger code excerpts for my example in a separate post StackOverflow.
> I hope that's not considered a faux pas for cross posting but it
> didn't seem these emails are a great place to copy and paste a bunch
> of code.
>
> http://stackoverflow.com/questions/6443498/pojos-collection-not-populated-when-submitting-form
>
> I also have a second related question.  My POJO's collection is a
> java.util.Set.  Does it matter to Struts 2 (or complicate things) that
> I'm using a Set and not a List?  Is it common to use a Set as a
> collection field in a POJO used in Struts 2?  The reason I'm using a
> Set is that I'm using Hibernate for ORM and according to the
> documentation a Set is the most common collection used association
> based mappings in Hibernate.
>
> <s:textfield name="flashCard.links.url" key="label.flashcard.link" size="66" />
>
> I've also tried:
> <s:textfield name="flashCard.links[0].url" key="label.flashcard.link"
> size="66" />
>
> From the Action class:
> FlashCard flashCard = new FlashCard();
>
> public FlashCard getFlashCard() {
>        return this.flashCard;
> }
>
> public void setFlashCard(FlashCard flashCard) {
>        this.flashCard = flashCard;
> }
>
> From the POJO:
> private Set<Link> links = new HashSet<Link>(0);
>
> public Set<Link> getLinks() {
>       return this.links;
> }
>
> public void setLinks(Set<Link> links) {
>      this.links = links;
>  }
>
> --
> Regards,
> Justin Robbins
>



-- 
Regards,
Justin Robbins
973.479.0448
JustinHRobbins@gmail.com

Learn more about me by viewing my professional profile on LinkedIn
http://www.linkedin.com/in/justinhrobbins

View my professional blog
http://javadeveloperjournal.blogspot.com/

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


Re: POJO's collection not populated when submitting form

Posted by Sewwandi <se...@gmail.com>.
Since you are using modeldriven and the model is FlashCard, i think the
following

 <sjr:tinymce
        id="flashCard.answer"
        name="flashCard.answer"
        key="label.flashcard.answer"
        rows="20"
        cols="50"
        editorTheme="simple"
    />

should be changed to

 <sjr:tinymce
        id="flashCard.answer"
        name="answer"
        key="label.flashcard.answer"
        rows="20"
        cols="50"
        value="answer"
        editorTheme="simple"
    />

the name field should be given without the prefix flashcard.also you
should provide the 'value' attribute in order for it to be
pre-populated.


thanks & regards
sewdil


On Thu, Jun 23, 2011 at 1:23 AM, Justin Robbins <ju...@gmail.com>wrote:

> Hi folks,
>
> I have a POJO which has a field of type java.util.Set that is
> collection of domain objects. When I submit a form to my Action, all
> the POJO fields are populated with values from the form except the
> field which is the collection. I have no idea why this collection
> isn't getting populated.  Any advice on how to resolve this problem or
> how to better troubleshoot it would be much appreciated.
>
> I'm posting some code excerpts below but please not that I posted
> larger code excerpts for my example in a separate post StackOverflow.
> I hope that's not considered a faux pas for cross posting but it
> didn't seem these emails are a great place to copy and paste a bunch
> of code.
>
>
> http://stackoverflow.com/questions/6443498/pojos-collection-not-populated-when-submitting-form
>
> I also have a second related question.  My POJO's collection is a
> java.util.Set.  Does it matter to Struts 2 (or complicate things) that
> I'm using a Set and not a List?  Is it common to use a Set as a
> collection field in a POJO used in Struts 2?  The reason I'm using a
> Set is that I'm using Hibernate for ORM and according to the
> documentation a Set is the most common collection used association
> based mappings in Hibernate.
>
> <s:textfield name="flashCard.links.url" key="label.flashcard.link"
> size="66" />
>
> I've also tried:
> <s:textfield name="flashCard.links[0].url" key="label.flashcard.link"
> size="66" />
>
> From the Action class:
> FlashCard flashCard = new FlashCard();
>
> public FlashCard getFlashCard() {
>        return this.flashCard;
> }
>
> public void setFlashCard(FlashCard flashCard) {
>        this.flashCard = flashCard;
> }
>
> From the POJO:
> private Set<Link> links = new HashSet<Link>(0);
>
> public Set<Link> getLinks() {
>       return this.links;
> }
>
> public void setLinks(Set<Link> links) {
>      this.links = links;
>  }
>
> --
> Regards,
> Justin Robbins
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: POJO's collection not populated when submitting form

Posted by Dave Newton <da...@gmail.com>.
IMO a set is user-hostile unless you've defined an iterator with known
sequence, otherwise the same page might return the same values in a
different order. That's just confusing.

(And I've never actually used a set in Hibernate, always a list or a map.)

I don't think collection syntax (square brackets, as you tried) or
indexed notation (paren) would work, since there's no getAt type thing
for a set.

You may be able to create a custom type converter, but meh.

Dave

On Mon, Jun 27, 2011 at 10:15 AM, Justin Robbins
<ju...@gmail.com> wrote:
> Hi folks,
>
> Not possible to populate a java.util.Set from a form post in Struts 2?
>  A discussion on Stack Overflow concluded that you can't populate a
> java.util.Set type Collection with a form post in Struts 2.  Can you
> guys confirm?
>
> Basically, I'm able to successfully iterate over a Set in a JSP to
> display the Collection but when I post a form to the Action all the
> POJO's fields get populated except the field that is of type
> java.util.Set.
>
> For those that use Hibernate I'm guessing this would be a common issue
> since Set is the most common collection mapping.
>
> http://stackoverflow.com/questions/6443498/pojos-collection-not-populated-when-submitting-form
>
> Many thanks for your input.
>
>
> On Wed, Jun 22, 2011 at 3:53 PM, Justin Robbins
> <ju...@gmail.com> wrote:
>> Hi folks,
>>
>> I have a POJO which has a field of type java.util.Set that is
>> collection of domain objects. When I submit a form to my Action, all
>> the POJO fields are populated with values from the form except the
>> field which is the collection. I have no idea why this collection
>> isn't getting populated.  Any advice on how to resolve this problem or
>> how to better troubleshoot it would be much appreciated.
>>
>> I'm posting some code excerpts below but please not that I posted
>> larger code excerpts for my example in a separate post StackOverflow.
>> I hope that's not considered a faux pas for cross posting but it
>> didn't seem these emails are a great place to copy and paste a bunch
>> of code.
>>
>> http://stackoverflow.com/questions/6443498/pojos-collection-not-populated-when-submitting-form
>>
>> I also have a second related question.  My POJO's collection is a
>> java.util.Set.  Does it matter to Struts 2 (or complicate things) that
>> I'm using a Set and not a List?  Is it common to use a Set as a
>> collection field in a POJO used in Struts 2?  The reason I'm using a
>> Set is that I'm using Hibernate for ORM and according to the
>> documentation a Set is the most common collection used association
>> based mappings in Hibernate.
>>
>> <s:textfield name="flashCard.links.url" key="label.flashcard.link" size="66" />
>>
>> I've also tried:
>> <s:textfield name="flashCard.links[0].url" key="label.flashcard.link"
>> size="66" />
>>
>> From the Action class:
>> FlashCard flashCard = new FlashCard();
>>
>> public FlashCard getFlashCard() {
>>        return this.flashCard;
>> }
>>
>> public void setFlashCard(FlashCard flashCard) {
>>        this.flashCard = flashCard;
>> }
>>
>> From the POJO:
>> private Set<Link> links = new HashSet<Link>(0);
>>
>> public Set<Link> getLinks() {
>>       return this.links;
>> }
>>
>> public void setLinks(Set<Link> links) {
>>      this.links = links;
>>  }
>>
>> --
>> Regards,
>> Justin Robbins
>>
>
>
>
> --
> Regards,
> Justin Robbins
> 973.479.0448
> JustinHRobbins@gmail.com
>
> Learn more about me by viewing my professional profile on LinkedIn
> http://www.linkedin.com/in/justinhrobbins
>
> View my professional blog
> http://javadeveloperjournal.blogspot.com/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

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


Re: POJO's collection not populated when submitting form

Posted by Justin Robbins <ju...@gmail.com>.
Hi folks,

Not possible to populate a java.util.Set from a form post in Struts 2?
 A discussion on Stack Overflow concluded that you can't populate a
java.util.Set type Collection with a form post in Struts 2.  Can you
guys confirm?

Basically, I'm able to successfully iterate over a Set in a JSP to
display the Collection but when I post a form to the Action all the
POJO's fields get populated except the field that is of type
java.util.Set.

For those that use Hibernate I'm guessing this would be a common issue
since Set is the most common collection mapping.

http://stackoverflow.com/questions/6443498/pojos-collection-not-populated-when-submitting-form

Many thanks for your input.


On Wed, Jun 22, 2011 at 3:53 PM, Justin Robbins
<ju...@gmail.com> wrote:
> Hi folks,
>
> I have a POJO which has a field of type java.util.Set that is
> collection of domain objects. When I submit a form to my Action, all
> the POJO fields are populated with values from the form except the
> field which is the collection. I have no idea why this collection
> isn't getting populated.  Any advice on how to resolve this problem or
> how to better troubleshoot it would be much appreciated.
>
> I'm posting some code excerpts below but please not that I posted
> larger code excerpts for my example in a separate post StackOverflow.
> I hope that's not considered a faux pas for cross posting but it
> didn't seem these emails are a great place to copy and paste a bunch
> of code.
>
> http://stackoverflow.com/questions/6443498/pojos-collection-not-populated-when-submitting-form
>
> I also have a second related question.  My POJO's collection is a
> java.util.Set.  Does it matter to Struts 2 (or complicate things) that
> I'm using a Set and not a List?  Is it common to use a Set as a
> collection field in a POJO used in Struts 2?  The reason I'm using a
> Set is that I'm using Hibernate for ORM and according to the
> documentation a Set is the most common collection used association
> based mappings in Hibernate.
>
> <s:textfield name="flashCard.links.url" key="label.flashcard.link" size="66" />
>
> I've also tried:
> <s:textfield name="flashCard.links[0].url" key="label.flashcard.link"
> size="66" />
>
> From the Action class:
> FlashCard flashCard = new FlashCard();
>
> public FlashCard getFlashCard() {
>        return this.flashCard;
> }
>
> public void setFlashCard(FlashCard flashCard) {
>        this.flashCard = flashCard;
> }
>
> From the POJO:
> private Set<Link> links = new HashSet<Link>(0);
>
> public Set<Link> getLinks() {
>       return this.links;
> }
>
> public void setLinks(Set<Link> links) {
>      this.links = links;
>  }
>
> --
> Regards,
> Justin Robbins
>



-- 
Regards,
Justin Robbins
973.479.0448
JustinHRobbins@gmail.com

Learn more about me by viewing my professional profile on LinkedIn
http://www.linkedin.com/in/justinhrobbins

View my professional blog
http://javadeveloperjournal.blogspot.com/

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