I have a text area and a text link in a jsp page,when I click this text link,I will call struts2 action by using ajax,like follows: /*-----jsp page-----*/ <html> <script type="text/javascript"> var http = getHttpObject(); function getHttpObject(){ var xmlhttp = false; if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); if(xmlhttp.overrideMimeType){ xmlhttp.overrideMimeType('text/xml'); } } else{ try{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){ try{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){ xmlhttp = false; } } } return xmlhttp; } function handleHttpResponse(){ if(http.readyState == READY_STATE_COMPLETE){ if(http.status == 200){ var text = http.responseText; if(text != null || text.length >0) alert(text); } else{ alert("error!!!"); alert(http.status); } } } function test(){ var t1=document.forms[0].abc.value; alert(t1); var url = "add.action"; http.open("POST",url,false); http.onreadystatechange = handleHttpResponse; http.send(null); } </script> <body> <s:form action='add.action' method='post'> <table> <tr><td><s:textfield label="test1" name="abc"/></td></tr> </table> <a href="javascript:test()">tt</a> </s:form> </body> </html>
/*----struts2 action----*/ public class TestAction extends ActionSupport{ String abc; public String add() throws Exception { System.out.println("abc="+abc); return null; } public String getAbc(){ return abc; } public void setAbc(String abc){ this.abc=abc; } } I put some text in textfield and then I click tt link,it call struts2 action,it execute System.out.println("abc="+abc),it should show the text value,but it shows null value!!! abc=null If I don't use ajax,and only use document.forms[0].submit(),struts2 can show right value,like follows: abc=the text you put I am puzzled with it! Why struts2 can't get jsp page value by using ajax? Anybody could tell me how to correct my above code? Thanks in advance!