This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Creating dynamic text boxes in Struts 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Creating dynamic text boxes in Struts 1" Watch "Creating dynamic text boxes in Struts 1" New topic
Author

Creating dynamic text boxes in Struts 1

Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 .


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

Thanks Anirvan

Its woking for me !!

Thanks again ..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Creating dynamic text boxes in Struts 1
 
Similar Threads
Struts - Check boxes problem
Selected values
Issue in submitting the Form with Arraylist in Struts 1.1
Action form that contains a collection of an unkown length
html tag