• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Form submission with duplicate

 
Ranch Hand
Posts: 240
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Kalichar Rangantittu
Ranch Hand
Posts: 240
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 240
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Kalichar Rangantittu
Ranch Hand
Posts: 240
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right. In this case, the tShirtSize property would have to be a double-dimensioned array, or of type String[][].
 
reply
    Bookmark Topic Watch Topic
  • New Topic