• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating dynamic text boxes in Struts 1

 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

I want to create dynamically text boxes in a Struts form and want to map those text box field into a form bean , So that i can read back those value in action class, Now my problem is I am not sure how many Text box variable i need to declare in my Form Bean class ?

Any help or suggestion is greatly appreciated .

Thanks in advance .
 
Anirvan Majumdar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One approach you can adopt is that you give all your textboxes the same value for the name attribute. And have getter/setter for the same name in your form bean. Just remember that instead of handling a String value, your getter/setter should handle a String[].
Something like:

This way you don't need to be concerned about how many textboxes are rendered. Be it a single or "n" number of textboxes, the values for each of them will get saved into the String[] of the form bean.
However, you'll need to put in some additional logic either at the client or server side, if it is important for you to map values present at different indices of the String[] to particular labels. Things you need to keep in mind:
1. If the user doesn't enter any value in any one of the textboxes, then there will not be a blank entry corresponding to it in the form bean's String[] variable.
2. The order in which the values are present in the String[] is directly related to the order in which the textboxes appear at the client side.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anirvan

Its woking for me !!

Thanks again ..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic