File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Creating dynamic text boxes in Struts 1" Watch "Creating dynamic text boxes in Struts 1" New topic

Creating dynamic text boxes in Struts 1

Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2904

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: 2904

Thanks Anirvan

Its woking for me !!

Thanks again ..
I agree. Here's the link:
subject: Creating dynamic text boxes in Struts 1
It's not a secret anymore!