• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

adding Dynamic text boxes using struts

 
Ranch Hand
Posts: 94
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry I have done some mistakes while replacing greater than,less tha signs using "& gt" and "<".
Please ignore those things.
Regards,
Ayan Dutta
 
Ranch Hand
Posts: 4864
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnaks Merill,
I really appreciate your detail explanation.

Regards

Naseem
 
Ayan Dutta
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Merrill . Many thanks for your help.
Ayan Dutta
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UseOneThreadPerQuestion
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
reply
    Bookmark Topic Watch Topic
  • New Topic