1. The map control of ArcGIS directly supports Ajax.
To use Ajax for map operations, perform the following operations:
Call the init method during body onload.
Function Init ()
{
VaR map = esricontrols. Maps ["map0"];
Map. addupdatelistener ("request", updateinforequest );
}
In this way, we will call the updateinforequest method when performing operations on the map.
Function updateinforequest (){
VaR url = esriutils. getserverurl ("frmmap ");
VaR map = esricontrols. Maps ["map0"];
VaR Params = "ajaxdemo = ajaxdemo & mapid = map0 &" + esriutils. buildrequestparams (Map. formid );
VaR XMLHTTP = esriutils. sendajaxrequest (URL, Params, true, function ()
{Updateinforesponse (XMLHTTP );});
}
Updateinforesponse is the callback function called when Ajax sends a request.
This function can be used to update our interface.
Function updateinforesponse (XMLHTTP ){
If (XMLHTTP! = NULL & XMLHTTP. readystate = 4 & XMLHTTP. Status = 200 ){
VaR xml = XMLHTTP. responsexml;
VaR scale = xml. getelementsbytagname ("scale"). Item (0). firstchild. nodevalue;
Document. getelementbyid ("scale"). value = "1:" + scale
}
}
Client calls
What does the server do?
We can use a class on the server to implement the phaselistener interface.
The Ajax lifecycle is executed independently on the server. This means that the browser sends a request with certain parameters and expects an XML response in a specific format. This response can be composed of any type of JSF phaselisteners, Servlet, or even static XML files. Now let's take a look at how to use phaselistener to process a request and return a response to the browser.
When the browser uses the XMLHTTPRequest object to send a request to the server, this request follows the standard JSF request processing lifecycle. These components are rebuilt in the restore view phase and their statuses are updated in the apply request values phase. Because we need to update the components on the page, we will skip the processing of the phaselistener. beforephase () method. In the phaselistener. afterphase method, we process the request and render the response to the client.
CodeAs follows:
Public class ajaxdemophaselistener implements phaselistener {
Public phaseid getphaseid (){
// Todo auto-generated method stub
// This indicates processing the application request Value
Return phaseid. apply_request_values;
}
Public void afterphase (phaseevent ){
Facescontext = phaseevent. getfacescontext ();
Externalcontext = facescontext. getexternalcontext ();
Map parammap = externalcontext. getrequestparametermap ();
String formid = (string) parammap. Get ("formid ");
String mapid = (string) parammap. Get ("mapid ");
// Determine whether the request is required. If not, return directly.
String ajaxdemo = (string) parammap. Get ("ajaxdemo ");
If (ajaxdemo = NULL |! Ajaxdemo. Equals ("ajaxdemo "))
Return;
Uicomponent form = facescontext. getviewroot (). findcomponent (formid );
Mapcontrol MC = (mapcontrol) form. findcomponent (mapid );
Webmap WM = mc. getwebmap ();
String scale = string. valueof (WM. getmapscale ());
Document Doc = xmlutil. newdocument ();
Element responseelement = xmlutil. createelement (Doc, "response", null, null );
Xmlutil. createelement ("scale", scale, responseelement );
Try {
Ajaxutil. writeresponse (facescontext, DOC );
//
Facescontext. responsecomplete ();
} Catch (ioexception E1 ){
// Todo auto-generated Catch Block
E1.printstacktrace ();
}
}
Public void beforephase (phaseevent ){
}
}
Finally, we register the listener in <lifecycle>.
In this way, the whole process of using ArcGIS Ajax is completed.