This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes List Objects in Form Bean has null values...mostly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "List Objects in Form Bean has null values...mostly" Watch "List Objects in Form Bean has null values...mostly" New topic
Author

List Objects in Form Bean has null values...mostly

Dan Keefe
Greenhorn

Joined: Apr 16, 2009
Posts: 6
Hi,
I'm sure I'm missing something obvious. I've spent tons of time reading about this problem, but I'm stuck. #6 in the Struts FAQ was the most helpful thus far.

I have a form bean with a list of beans. When the save and submit actions I have are executed, the form bean values are all populated correctly except the list. The fields in the list object are all null except the ones that I've defined as textarea elements with indexed="true". I've tried changing the scope from "request" to "session", but no change. I can see this when I put a breakpoint in my validate method.

Here is the relevant code from my Struts 1.1 application. I've tried to trim out the fat. There are actually many more fields in each bean.

struts-config


form bean


list bean


JSP


When I break at my validate method, partNumber will be null and suggestion will be correctly populated. I've also tried many other ways of writing the list fields in the JSP (hidden fields, using property='<%="partNumber["+idx+"]"%>', etc) , but most of them make it so it doesn't render. Please let me know what I'm doing wrong!
Thank you,
Dan
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

This chunk of code perplexes me--I'm assuming index is never >= itemList.size(), otherwise wouldn't this loop until the computer melted?

Have to you verified that the indexed textarea is rendering correctly?

Why would partNumber be anything but null? Is there a form field or initialization for partNumber you haven't shown? All I see (with an admittedly hasty overview) is a <bean:write> tag.
Dan Keefe
Greenhorn

Joined: Apr 16, 2009
Posts: 6
David Newton wrote:This chunk of code perplexes me--I'm assuming index is never >= itemList.size(), otherwise wouldn't this loop until the computer melted?

Have to you verified that the indexed textarea is rendering correctly?

Why would partNumber be anything but null? Is there a form field or initialization for partNumber you haven't shown? All I see (with an admittedly hasty overview) is a <bean:write> tag.


The perplexing code is straight from the FAQ:
http://faq.javaranch.com/java/IndexedProperties
I just pasted it in without even looking at it for the sake of solving this issue. :o

The indexed textareas are rendering fine and the attributes are retaining their value. (Indexed text fields work fine too.) I just noticed now that if I add 'disabled="true"', these attributes also are null in the requests.

partNumber and suggestion are both populated via GetRfqAction. partNumber can be "" and suggestion can be null, but they are both populated in the response and visible on the page.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Disabled form fields, IIRC, aren't sent by the browser.

I don't see where the partNumber is being populated, so I won't be able to help much more: in the JSP posted there's just a <bean:write>, so I wouldn't expect that field to be populated on form submission since there's no form element.
Dan Keefe
Greenhorn

Joined: Apr 16, 2009
Posts: 6
It's being populated via GetRfqAction:
I know it's populated via this: BeanUtils.copyProperties(rfqForm, rfqDTO); I've inspected the code with breakpoints and it's also passed correctly to my JSP and displayed.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

IIRC copyProperties doesn't do a deep copy (could be wrong about that).

That aside, there's still no form elements for the partNumber, so it wouldn't be set on form submission.
Dan Keefe
Greenhorn

Joined: Apr 16, 2009
Posts: 6
David Newton wrote:IIRC copyProperties doesn't do a deep copy (could be wrong about that).

That aside, there's still no form elements for the partNumber, so it wouldn't be set on form submission.


It does do a deep copy. It has the same effect as rfqForm.setItemList(rfqDTO.getItemList());

I don't know what you're saying regarding no form elements. I'm pretty new to all of this so maybe that's my problem.
If I change my JSP to have:
<html:text name="item" property="partNumber" indexed="true"/>
It is populated correctly.

So let me rephrase my question:
How do I get all of the attributes in my list in my form to be populated without putting them into editable text attributes? partNumber will not be editable on the screen to the user, but I do want the same value passed back when I save/submit. As I said before, it only comes back when I have it in a text-like box. I haven't been successful with hidden elements. Is that what I need to use?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Put them in a hidden form element? <bean:write> isn't a form element, it just displays values--when a form is submitted only form elements are submitted, not random text on the page.

FYI, from http://commons.apache.org/beanutils/v1.8.0/apidocs/index.html:
Note that this method is intended to perform a "shallow copy" of the properties and so complex properties (for example, nested ones) will not be copied.
Dan Keefe
Greenhorn

Joined: Apr 16, 2009
Posts: 6
David Newton wrote:Put them in a hidden form element? <bean:write> isn't a form element, it just displays values--when a form is submitted only form elements are submitted, not random text on the page.

FYI, from http://commons.apache.org/beanutils/v1.8.0/apidocs/index.html:
Note that this method is intended to perform a "shallow copy" of the properties and so complex properties (for example, nested ones) will not be copied.


I guess I don't know how to put these items in a hidden form element. I've tried:
<html:hidden property="<%="partNumber["+idx+"]"%>" />
and I get:
No getter method for property partNumber[0]

Here is a slimmed down version of my JSP




I did test copyProperties, but I found that it already had copied those properties by the time it hit my rfqForm.setItemList(rfqDTO.getItemList()); statement so I removed that statement. Maybe I'll put it back in given what you've pointed out in the javadoc.

Thanks
Dan Keefe
Greenhorn

Joined: Apr 16, 2009
Posts: 6
I guess I am just syntactically challenged. I add this to my iterate statement and it works just like I want it to.


Thanks for trying to help, David. :beerchug:
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

IIRC the <html:hidden> tag also has an "indexed" attribute, too.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: List Objects in Form Bean has null values...mostly
 
Similar Threads
DispatchAction and Validator
html:select error occuring
Problem with ActionForm
web portal in struts and nothing is working at all
No action is occured when i click the delete link to delete.