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 how to create property,setter getter for TextBox field whose name would be dynamically created? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to create property,setter getter for TextBox field whose name would be dynamically created?" Watch "how to create property,setter getter for TextBox field whose name would be dynamically created?" New topic
Author

how to create property,setter getter for TextBox field whose name would be dynamically created?

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 639
I have a Struts 2 Form from which I am entering fields including Email IDs.Now user can have click on add to dynamically add new text box for entering more email Ids. So the names of textboxes in such a case would be created on user click of add more button. But the problem is that field names in Struts 2 have to be properties of Action class with setter getter methods. But how can i create property and setter getter for a textbox whose name is not yet known. ? Suppose there are 5 fields as text box and I have 5 properties in action class with setter getter.Now I click add button and dynamically generate a new text box for additional emailId.How can i have this name as property of action class when this name is known only dynamically on click of add button?

thanks
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8866
    
    8

Use an indexed property (yes, that page is for Struts 1.x, but the principal applies in Struts 2.x). Basically the getter/setter on your action or data object should take a collection and the name of your text box should be the collection name plus an index.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 639
Thanks.Apart from that I read one more post:
webpage

In this I saw the idea that create dynamic text box create them as textBox1,textBox2 and so on and use the same name in FormBean( in Action Class only in struts 2).


I tried the below:




Idea is to use same name in Action Class property.
The above works but below does not:





Now I would use the name EmailId1 as property but the above does not work.Please advice.

Thanks.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8866
    
    8

Are you looking at this example? Notice that your code does not look like the code in the example.
You also haven't shown your action. I assume your getter and setter takes a collection.
Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 253
Hi All,

I've same requirement now. I've a bundle of text boxes in one <li><table><tr>1</tr><tr>2</tr><tr>3</tr><tr>...</tr></table></li>. So, when I click on add button, it produces another <li><table>bundle of text inputs</table></li>. My problem is, how to create dynamic setters and getters for all inputs. here is my all source code. Please guide me or modify the code, the way it has to be.

Action.class



Struts.xml


index.jsp



collection class:



bean class



DBstore class.




Please reply as soon as possible


Thanks:
Ramakrishna K.C
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8866
    
    8

Ramakrishna Udupa wrote:
Please reply as soon as possible


Please do not post the same question more than once. I've already linked to a relevant example in your other post. Since you found Monica's post you can obviously search the forum for other examples, of which there are many. We will not do your work for you. If you show some effort we would be glad to help you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to create property,setter getter for TextBox field whose name would be dynamically created?