| 发表于:2007-03-05 09:56:514楼 得分:0 |
---------------- ajax ---------------- /** * ajax.js * * collection of scripts to allow in page communication from browser to (struts) server * ie can reload part instead of full page * * how to use * ========== * 1) call retrieveurl from the relevant event on the html page (e.g. onclick) * 2) pass the url to contact (e.g. struts action) and the name of the html form to post * 3) when the server responds ... * - the script loops through the response , looking for <span id= "name "> newcontent </span> * - each <span> tag in the *existing* document will be replaced with newcontent * * note: <span id= "name "> is case sensitive. name *must* follow the first quote mark and end in a quote * everything after the first '> ' mark until </span> is considered content. * empty sections should be in the format <span id= "name "> </span> */ //global variables var req; var which; /** * get the contents of the url via an ajax call * url - to get content from (e.g. /struts-ajax/sampleajax.do?ask=command_name_1) * nodetooverwrite - when callback is made * nameofformtopost - which form values will be posted up to the server as part * of the request (can be null) */ function ajax(url,nameofformtopost) { //get the (form based) params to push up as part of the get request //url= "/ "+url+ ".do? "+getformasstring(nameofformtopost); url= "/ "+url+ "? "+getformasstring(nameofformtopost); alert(url); //do the ajax call if (window.xmlhttprequest) { // non-ie browsers req = new xmlhttprequest(); req.onreadystatechange = processstatechange; try { req.open( "get ", url, true); //was get } catch (e) { alert( "problem communicating with server\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(); } } } /* * set as the callback method for when xmlhttprequest state changes * used by retrieveurl */ function processstatechange() { if (req.readystate == 4) { // complete alert(req.statustext); if (req.status == 200) { // ok response alert(req.statustext); ///alert( "ajax response: "+req.responsetext); //split the text response into span elements spanelements = splittextintospan(req.responsetext); //use these span elements to update the page replaceexistingwithnewhtml(spanelements); } else { alert( "problem with server response:\n " + req.statustext+ "\n--------------------\nreq.status= "+req.status); } } } /** * gets the contents of the form as a url encoded string * suitable for appending to a url * @param formname to encode * @return string with encoded form values , beings with & */ 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 form /strutsaction.do&name=value for ( var i=formelements.length-1; i> =0; --i ){ //we escape (encode) each value if(formelements[i].name != "submit " && formelements[i].name != "cancel ") returnstring=returnstring+escape(formelements[i].name)+ "= "+escape(formelements[i].value)+ "& "; } //return the values return returnstring.substr(0,returnstring.length-1); } /** * splits the text into <span> elements * @param the text to be parsed * @return array of <span> elements - this array can contain nulls */ function splittextintospan(texttosplit){ //split the document returnelements=texttosplit.split( " </span> ") //process each of the elements for ( var i=returnelements.length-1; i> =0; --i ){ //remove everything before the 1st span spanpos = returnelements[i].indexof( " <span "); //if we find a match , take out everything before the span if(spanpos> 0){ substring=returnelements[i].substring(spanpos); returnelements[i]=substring; } } return returnelements; } /* * replace html elements in the existing (ie viewable document) * with new elements (from the ajax requested document) * where they have the same name and are <span> elements * @param newtextelements (output of splittextintospan) * in the format <span id=name> texttoupdate */ function replaceexistingwithnewhtml(newtextelements){ //loop through newtextelements for ( var i=newtextelements.length-1; i> =0; --i ){ //check that this begins with <span if(newtextelements[i].indexof( " <span ")> -1){ //get the name - between the 1st and 2nd quote mark startnamepos=newtextelements[i].indexof( ' " ')+1; endnamepos=newtextelements[i].indexof( ' " ',startnamepos); name=newtextelements[i].substring(startnamepos,endnamepos); //get the content - everything after the first > mark startcontentpos=newtextelements[i].indexof( '> ')+1; content=newtextelements[i].substring(startcontentpos); //now update the existing document with this element //check that this element exists in the document if(document.getelementbyid(name)){ //alert( "replacing element: "+name); document.getelementbyid(name).innerhtml = content; } else { //alert( "element: "+name+ "not found in existing document "); } } } } --------------------------------------------------------------------------- 請問我要如何在jsp的頁面上顯示actionformj裏actionerrors信息?(這裏是用了ajax提交的) | | |
|