Two Laptop Bag*
The moose likes Struts and the fly likes adding Dynamic text boxes using struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "adding Dynamic text boxes using struts" Watch "adding Dynamic text boxes using struts" New topic
Author

adding Dynamic text boxes using struts

Ayan Dutta
Ranch Hand

Joined: Oct 16, 2005
Posts: 94
Hi I want to build a page where each the user will Press the Add button ,one text box will be added in the page .For this my code is like ..

function generateRow() {

var d=document.getElementById("div");
d.innerHTML+="<input type='text' name='field1'>";
d.innerHTML+="<input type='text' name='field2'>" ;

}


<body>
<div id="div"></div>
<input type ="button" name="Add" on click="generateRow()"

</body
</html

This one is working fine .But I cant access these text box values using form beans .So when I am trying to write the function generateRow() as


function generateRow() {
getElementById("div");
div.innerHTML+="<html:text property="field1">" ;
div.innerHTML+="<html:text property="field2">" ;

}

It is not working .

I am sure I am doing something coceptually wrong .First thing I want to know
where I am doing the mistake .Second thing is that I want to know if there is any work around for adding struts text boxes(i.e html:text,not normal text boxes) in the page dynamically using Javascript ?
Regards,
Ayan
Ayan Dutta
Ranch Hand

Joined: Oct 16, 2005
Posts: 94
sorry I have done some mistakes while replacing greater than,less tha signs using "& gt" and "<".
Please ignore those things.
Regards,
Ayan Dutta
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
There are two problems here:

1-JavaScript only acts on the Plain HTML tags that are rendered and sent back to the browser, not on Struts tags. Therefore, trying to dynamically enter an <html:text> tag through JavaScript is never going to work.

2-If your form has multiple rows of data, each of which has a "field1", that isn't going to work either. Struts has got to be able to tell which field to populate in the form bean, and if they're all named "field1" it won't be able to tell.

In order to code this properly, you are going to have to thoroughly understand how indexed properties work in Struts. This link should help you understand how they work.

Also, since you're dynamically expanding the list, you will need to use a Lazy Initialization approach in your ActionForm to be able to accomodate the new rows of data.


Merrill
Consultant, Sima Solutions
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Hi Merrill, Will this code work?

If I can get the form object from javascript, then can we add struts text field in that same <html:form>



Please ignore this-->> onklick Assume onclick instead of onklick

Thanks in advance

Regards

Naseem
[ July 03, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You have to realize that when this form is rendered and sent back to the browser, it no longer has an <html:form> tag. It has a <form> tag. It doesn't have an <html:button> tag, it has a <input type="button"> tag.

When writing JavaScript for Struts JSP pages, it is helpful to use your browser's "view source" function. In this way, you can see the HTML that the JavaScript is actually acting upon.

Your JavaScript can certainly add rows to the page. Just remember whenever you use innerHTML, it has to be standard HTML, not Struts tags.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Thnaks Merill,
I really appreciate your detail explanation.

Regards

Naseem
Ayan Dutta
Ranch Hand

Joined: Oct 16, 2005
Posts: 94
Thanks Merrill . Many thanks for your help.
Ayan Dutta
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Did you get it working? A few years ago, before I came across the idea of indexed properties, I had a similar challenge. It was a page where the user could enter many phone numbers and clicking a + button would show another entry on the screen. The "solution" was to limit the number of phone numbers to 10 and create properties named like phoneType1, phoneNumber1, phoneType2, phoneNumber2, etc. Yea it is a bit ugly and would not work if there was the potential for a lot of entries. I have though about going back to that page and updating it to use indexed properties, but it has never been an issue.

- Brent
sanju sreedharan
Ranch Hand

Joined: Nov 25, 2008
Posts: 48
i am going through the same problem now..can anyone tell me about indexed property with an example..i am new to struts please help me

sanju
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

UseOneThreadPerQuestion
Varun Annand
Greenhorn

Joined: Nov 06, 2011
Posts: 6
I have got a solution to the problem:
How to add dynamic textboxes to a Struts jsp page that uses DynaActionForm:
I have used indexed property with multiple submit buttons.
In DynaActionForm, I have used a String[] named userName[] with size of 1.
But, I change the size dynamically in the Action class.
I have added the textboxes dynamically using a scriptlet tag, based on value of counter which is set in Action class and saved in the request object of execute method of Action class.
I have set the scope of Action as session in struts-config.xml file.
There are three submit buttons, one each for adding a textbox, removing a text box and submitting the form. Each submit button has a name and a value associated with it.
Name of submit button is taken as a <form-property-> with type java.lang.String in DynaActionForm.
The value associated with the submit button's property is set automatically and is compared in the Action class to check which submit button is pressed and to execute code accordingly.


<<<<<<<<<<It will get confusing if I keep explaining like this.><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<It is simpler than it sounds.><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<So I am attaching the program which I made using Eclipse Helios and Struts 1.1><<<<<<<<<<<<
<<<<<<<<<<In my code, Robin is a valid userName><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<It does not add another textbox unless you type something in previous one><<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<<<<<<<<<<<<<Sorry guys, zipped folders are not allowed on this website<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<<<<<<<<<<<<<I have uploaded it at this url: Dynamic TextBox using Struts 1.1 and Eclipse Helios

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Sorry guys, this website asks to create an account to download but it is free and I found no better option <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Please watch that the zipped file 'myStruts1.zip' gets downloaded automatically without prompting once you create account on "keepandshare.com">
<<<I have shared the document publicly, so I hope that you can download it even without creating account<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
Consider Paul's rocket mass heater.
 
subject: adding Dynamic text boxes using struts