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 Form submission with duplicate 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 "Form submission with duplicate" Watch "Form submission with duplicate" New topic
Author

Form submission with duplicate

Kalichar Rangantittu
Ranch Hand

Joined: Jan 15, 2002
Posts: 240
I am having to submit a form that has the following:
<!-- First entry -->
<input type="text" name="firstName"/>
<input type="text" name="lastName"/>

<!-- second entry-->
<input type="text" name="firstName"/>
<input type="text" name="lastName"/>
.....
<!-- nth entry -->
<input type="text" name="firstName"/>
<input type="text" name="lastName"/>

When the form is submitted, I can associate the appropriate first name with the last by array index. I cannot have unique names for the first and last name entries as I might have multiple sets of the same.

This model works fine for me currently. Now, if I were to place an <option> selection as part of each set, how can I make this association?

Thanks in advance. Please assist.


Never be satisfied with anything less than the best and you will surely pass the test...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

The order that the parameters are delivered is not guaranteed. Even though it appears to be working for you now, that could change in the future and is a risk I would not take.

Usually, when faced with such a scenario, I would use something along the lines of:



to guarantee the association.

It's more work on the server, but you never have to worry about things changing underneath you and mixing everything up.

Extending this to other controls should be an easy leap.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kalichar Rangantittu
Ranch Hand

Joined: Jan 15, 2002
Posts: 240
Thanks for the response. I am using Struts to handle the form population. If I define an array in the form such as
String firstName[];
String lastName[];
with set/gets, BeanUtils.copyProperties() fails.

I am hoping not to have individual elements defined like firstName1 and having getFirstName1() etc due to the dynamic nature of the elements.

Thanks once again. Any tips?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

Does Struts not have a concept of a dyanmic form to handle just such situations?

I've moved this to the Struts forum for people who use it to comment.
Kalichar Rangantittu
Ranch Hand

Joined: Jan 15, 2002
Posts: 240
I was not using setFirstName(int index, String val).Its working great now. I however am still unsure as to how to get this to work if I have an options field as part of each firstName/lastName set and how to associate it with the right element.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You woulld use the same principle in creating a dropdown box for each name. Suppose you wanted to provide a field for t-shirt size with options samll, medium or large for each name. Here's how you'd do it

In your JSP:

<select name="tShirtSize[0]" >
<option>small</option>
<option>medium</option>
<option>large</option>
</select>

<select name="tShirtSize[1]" >
<option>small</option>
<option>medium</option>
<option>large</option>
</select>


Then, in your ActionForm, code a setTShirtSize(int index, String size) method.

Just a note on style: This method, while it works, is not making very good use of object oriented design. A better design would be to have a Person object with properties for first name, last name, t-shirt size, etc. Then have an array or an ArrayList of Person objects in your ActionForm.
[ June 14, 2006: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Kalichar Rangantittu
Ranch Hand

Joined: Jan 15, 2002
Posts: 240
Merill,

Thanks much for the response. The example you have provided works fine if the selection is of one single element from the options. If I were to select multiple, then wouldnt I need to pass in an array to
setOption(int index, String[] value) ?

Thanks
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Yes, you are right. In this case, the tShirtSize property would have to be a double-dimensioned array, or of type String[][].
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Form submission with duplicate
 
Similar Threads
any othe way to getParameters?
Calling Java Bean from JSP
database best practice
jsp:useBean id not clear
form in multilingual