Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

struts html:form with javascript for ajax

Thread view: 
eunever32@yahoo.co.uk - 27 Feb 2007 23:01 GMT
Hi

I have a struts (1.1) form

<html:form

<html:text
<html:text ...

</html:form>

All was well until I wanted to add an ajax field that would do auto-
complete.
Previously working example had
<input id="complete-field"

This was incompatible with the html:form tag and so I continued to use
<input

but when I submit the form the value of the input field is null

How can I modify my form so that I can use the javascript
functionality ?

Thanks
Lew - 28 Feb 2007 04:14 GMT
> Hi
>
[quoted text clipped - 16 lines]
>
> but when I submit the form the value of the input field is null

Does your input field have a name as well as an id?

Struts does some special magic to pull request parameters into its Form
classes. If you don't use its html: tag lib than you might need to make
explicit request.getParameter() calls in the Action class for those parameters.

- Lew
Shashanka.Rao@gmail.com - 28 Feb 2007 08:28 GMT
On Feb 28, 4:01 am, euneve...@yahoo.co.uk wrote:
> Hi
>
[quoted text clipped - 21 lines]
>
> Thanks

Using Ajax you will be passing all the request parameters through GET
and the there is no form.submit() happening. This may be one of the
reasons. In that case only way to send these parameters is to build
the URL string with the request parameters. Look at the code below to
build your URL.

function retrieveURL(url,nameOfFormToPost) {

 //convert the url to a string
 url=url+getFormAsString(nameOfFormToPost);

 //Do the AJAX call
 if (window.XMLHttpRequest) {

   // Non-IE browsers
   req = new XMLHttpRequest();
   req.onreadystatechange = processStateChange;
   try {
         req.open("GET", url, true);
   } catch (e) {
     alert("Server Communication Problem\n"+e);
   }
   req.send(null);
 } else if (window.ActiveXObject) {
   // IE

   req = new ActiveXObject("Microsoft.XMLHTTP");
   if (req) {
     req.onreadystatechange=processStateChange;
     req.open("GET", url, true);
     req.send();
   }
 }
}

getFormAsString() is a "private" method used by the retrieveURL()
method. This will get all the form Elements and appends to the URL

function getFormAsString(formName){

 //Setup the return String
 returnString ="";

 //Get the form values
 formElements=document.forms[formName].elements;

 //loop through the array, building up the url
 //in the format '/strutsaction.do&name=value'

 for(var i=formElements.length-1;i>=0; --i ){
       //we escape (encode) each value
       returnString+="&"
       +escape(formElements[i].name)+"="
       +escape(formElements[i].value);
}

//return the values
return returnString;
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.