struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Frank W. Zammetti" <fzli...@omnytex.com>
Subject Re: DHTML and Struts
Date Wed, 02 Aug 2006 14:49:37 GMT
Any form elements created dynamically on the client-side will have no
intrinsic link to the ActionForm.  However, this is not necessarily a
problem... imagine if your ActionForm has this in it:

private String firstName;
public void setFirstName(String inFirstName) {
  firstName = inFirstName;
}
public String getFirstName() {
  return firstName;
}

What happens if your JSP *DOES NOT* include this form field?  Obviously
that will be just fine, Struts won't complain.

Now, what happens if you dynamically add that field to your HTML form via
JavaScript, and then submit the form?  Again, this will be just fine,
Struts will happily populate the firstName field in the ActionForm.  It
doesn't matter that it wasn't there when the HTML for the page was
originally rendered by the JSP.

In the case of indexed properties, the same is true... if you dynamically
add a field to the HTML form, so long as the name follows the index naming
paradigm, it will be populated in the ActionForm when submitted.

The code you have here looks basically correct, with one possible
exception... setting innerHTML doesn't necessarily add anything to the
DOM.  So, when the form is submitted, the fields you dynamically added may
not be sent (I believe it will work in some browsers, but not in others...
I'd have to go test to verify this, but that's what's in my memory). 
Instead, you should use DOM methods to create your new field and append it
to the form, that should alleviate that problem.  This is generally the
preferred method to work with dynamic content anyway.  So, your add method
should be something like

function add() {
  var newField = document.createElement("input");
  newField.type = "text";
  newField.name = "foo[" + count + "]";
  var newCell = document.createElement("td");
  newCell.appendChild(newField);
  var newRow = document.createElement("tr");
  newRow.appendChild(newCell);
  var theTable = document.getElementById("t1");
  theTable.appendChild(newRow);
  count++;
}

Frank

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM/Yahoo: fzammetti
MSN: fzammetti@hotmail.com
Author of "Practical Ajax Projects With Java Technology"
 (2006, Apress, ISBN 1-59059-695-1)
Java Web Parts - http://javawebparts.sourceforge.net
 Supplying the wheel, so you don't have to reinvent it!

On Wed, August 2, 2006 12:48 am, Puneet Lakhina wrote:
>>
>>
>> you were hung up (maybe I read into your question incorrectly).  So are
>> you actually hung up on the JavaScript to dynamically add fields?
>
>
>
> Yes, i am able to add text fields to my page, but I dont know what all
> attributes to specify in the input tag so that my user bean recieves the
> values from the dynamically added text fields.
>
> i.e.
> <script language="javascript>
> count=1;
> function add()
> {
> var row = document.getElementById('t1').insertRow(count);
> var cell=row.insertCell(0);
> cell.innerHTML="<input type=text name=foo[" + count + "]" +  "/>
> count++;
> }
> </script>
> <input type="button" onclick="add()" />
> <table id="t1">
> <tr>
>   <td>
>     <html:text property="foo[0]" >
>   </td>
>  </tr>
> </table>
> </table>
>
> --
> Puneet
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message