File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes can't get indexed form property to work. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "can Watch "can New topic
Author

can't get indexed form property to work.

Brian Harper
Greenhorn

Joined: Dec 31, 2008
Posts: 6
So I've looked at the FAQ and it seems like I should be able to solve this from there, but I'm still struggling to get it to work.

I can't get my input fields to show when first loading my submit page.

I have the following form:
Main Form: SubmitForm, it's a large form with an indexed property in it that I can't get to work:



(it has the appropriate getter/setters)



(with appropriate getters/setters
struts-config.xml:



JSP: - as you can see, i've tried both forEach and iterate.



Neither seems to populate the initial table in the jsp to 5 rows. The iterate doesn't find anything to iterate over.

Any suggestions?
Thanks
[ December 31, 2008: Message edited by: Brian Harper ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

"Twivel", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Brian Harper
Greenhorn

Joined: Dec 31, 2008
Posts: 6
To be a bit clearer on my problem: The jsp compiles fine, the page displays, no errors show up in the log, it just doesn't populate the empty text fields in the jsp.

I suspect this is because there is no data in SubmitForm.ingredients because it hasn't been filled out yet, but I expected having size="5" in the <form-bean> tag would cause it to show 5 empty rows.

Clearly, I don't have something configured right.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Code looks good to me, Just some changes/doubts,

1. Change JSP code name attribute , like


I changed the name attribute, because the the mapping of name in form-bean tag is


2. Are you using DynaActionForm ? Because you declared form-property element in your form-bean tag



[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Brian Harper
Greenhorn

Joined: Dec 31, 2008
Posts: 6
Ok, I changed the name and am still having the same issue... the iterator does nothing. I think your second comment is the cause. I guess I was just confused about when to use the form-property tag, I don't use DynaActionForm.

My flow is as follows:

1. Action /submit.do is a forward to submit.jsp without an action type.
2. Clicking submit does onsubmit validation and calls a different action: /submitAction.do to do server side validation and processing - and this does have an action type.

I have two actions because I don't want validation (and the associated errors) with showing the form the first time. Perhaps there is a better approach to this?

So without using a DynaActionForm, I guess I need to have a different action associated with 1 that pre-populates the form so the rows show up.... off to try it now.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Originally posted by Brian Harper:
I guess I was just confused about when to use the form-property tag, I don't use DynaActionForm.


This is what struts-config_1_2.dtd says about "form-property" element:

The "form-property" element describes a JavaBean property that can be used to configure an instance of a DynaActionForm or a subclass thereof. This element is only utilized when the "type" attribute of the enclosing form-bean" element is [org.apache.struts.action.DynaActionForm] or a subclass of DynaActionForm. If a custom DynaActionForm subclass is used, then the "dynamic" attribute of the enclosing <form-bean> element must be set to "true". Since Struts 1.1.


So If you are not using DynaActionForm, then remove that element.

1. Action /submit.do is a forward to submit.jsp without an action type.


Now, While forwarding to JSP page on which you are iterating , then simply add the name of "bean" in action mapping tag..
like

<action .. name="submitForm"/>

A question : Any special reason to use array of size 5(You can show 5 empty rows by iterating 5 times), If no, then have look at this FAQ, It might help !!
Brian Harper
Greenhorn

Joined: Dec 31, 2008
Posts: 6
I've got it working now, I stopped using the array example and followed the FAQ exactly. I had looked at it before, but I was still working off of a different example that used an array.

And I guess you're right, it makes more sense to just iterate in the JSP if the List is empty than to add empty elements to the list.

I'd also like to expand the list dynamically without submitting.... (e.g. add another row by clicking a button). this seems to help with that, I just need to figure out how to get the last index so I can increment it. Any tips on that?

Thanks for the help.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Originally posted by Brian Harper:
I just need to figure out how to get the last index so I can increment it. Any tips on that?


Why you required last index position ?

Still, <logic:iterate has "indexId" attribute which holds the current index of the collection on each iteration.

Look here.
Brian Harper
Greenhorn

Joined: Dec 31, 2008
Posts: 6
Originally posted by Sagar Rohankar:

Why you required last index position ?


My current thoughts are:

On the server side, I will iterate over the collection and create the initial table and one empty row.

Then, on the client side I want to provide a button that allows them to
add more rows after they fill in the empty one. I can use javascript but take a look at the following example:



In the above code, javascript innerHTML will have to know the last X used so it can increment it.

Thanks for your help!
Brian Harper
Greenhorn

Joined: Dec 31, 2008
Posts: 6
I've figured out how to answer the number of rows in the table question.

Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

I'm happy to help you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can't get indexed form property to work.
 
Similar Threads
Indexed properties and logic:iterate problem
html:text doesn't write the value to my object
indexed property
Generate Radio Buttons
ArrayList inside an ArrayListVO