struts html:form with javascript for ajax

E

eunever32

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
 
L

Lew

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

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
 
S

Shashanka.Rao

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

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.name)+"="
+escape(formElements.value);
}

//return the values
return returnString;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top