小秋的家

          home

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            1 Posts :: 91 Stories :: 17 Comments :: 0 Trackbacks

           

          1.在頁面端,首先撰寫自定義的javascript方法,有了這個方法,就可以在前端頁面的任何地方去觸發地圖事件了:

          function test(str){

              //需要傳遞到后臺的自定義參數

              var str1=encodeURI(encodeURI(str));


              //“mapForm”是地圖頁面上的表單名,從該表單獲取向后臺提交的URL

              var url = EsriUtils.getServerUrl("mapForm");



              //“map1”是地圖頁面上的地圖ID

              var map = EsriControls.maps["map1"];


                //定義需要傳遞的參數列表,最后的“EsriUtils.buildRequestParams(map.formId)”為固定寫法

              var params = "addpointInt=addpointInt";//這個參數用來在后臺判斷當前應該做什么操作,從而決定調用哪個處理類,參數名和值都是自定義的

              params += " &mapId=map1 ";//地圖的ID

              params += " &str="+str1;//自定義的參數

              params += " &"+EsriUtils.buildRequestParams(map.formId);//固定寫法

             

              //調用sendAjaxRequest方法,發送請求,其中的function(){testCallBack(xmlHttp);}為回調方法

              var xmlHttp = EsriUtils.sendAjaxRequest(url,params,true,function(){testCallBack(xmlHttp);});

          }


          2.然后是相關的回調方法,回調方法就可以根據自己的需要隨意進行操作了,這只是非常普通的AJAX回調方法

          function testCallBack(xmlHttp){

              if (xmlHttp != null && xmlHttp.readyState == 4 && xmlHttp.status == 200) {

                  var xml = xmlHttp.responseXML;

                  var map = EsriControls.maps["map1"];

                  // map.refresh();

                  // alert("run here");

                  map.resize(map.bounds.width, map.bounds.height);

              }

          }


          3.然后是撰寫后臺程序,用以捕捉前臺所觸發的AJAX事件。這是一個listener,需要實現接口javax.faces.event.PhaseListener:

           

          import java.util.Map;

           

          import javax.faces.context.ExternalContext;

          import javax.faces.context.FacesContext;

          import javax.faces.event.PhaseEvent;

          import javax.faces.event.PhaseId;

          import javax.faces.event.PhaseListener;

           

          public class SelfPhaseListener implements PhaseListener {

                  //////////////////////////////ajax處理代碼

              public PhaseId getPhaseId() {

                  // TODO Auto-generated method stub

           

                  //這里表示在應用請求值階段進行處理

                  return PhaseId.APPLY_REQUEST_VALUES;//.APPLY_REQUEST_VALUES;//選擇action的回調處理過程

              }

               

               

              public void afterPhase(PhaseEvent phaseEvent) {

                  //從FacesContext中獲得由前端javascript方法傳遞來的參數列表

                  FacesContext facesContext = phaseEvent.getFacesContext();

                  ExternalContext externalContext = facesContext.getExternalContext();

                  Map paramMap = externalContext.getRequestParameterMap();

                 


                  //這里判斷是不是我們需要的請求,如果不是直接返回。這個“addpointInt”即是先前所定義的javasctipt方法中的一個參數

                  if("addpointInt".equals((String) paramMap.get("addpointInt"))){

                              //這個AddPointPhaseListener類就是自定義的用來進行后臺處理的類

                      AddPointPhaseListener addpoint=new AddPointPhaseListener();

                      addpoint.excute(paramMap, facesContext);

                  }

               }

           

           

              @Override

              public void beforePhase(PhaseEvent arg0) {

                 // TODO Auto-generated method stub

                

              }

          }


          4.接下來寫一個用來處理具體業務的類:

          import java.util.Map;

           

          import javax.faces.component.UIComponent;

          import javax.faces.context.FacesContext;

           

          import org.w3c.dom.Document;

          import org.w3c.dom.Element;

           

          import com.esri.adf.web.data.TocFunctionality;

          import com.esri.adf.web.data.WebContext;

          import com.esri.adf.web.data.WebMap;

          import com.esri.adf.web.faces.component.MapControl;

          import com.esri.adf.web.faces.renderkit.xml.ajax.AJAXUtil;

          import com.esri.adf.web.util.XMLUtil;

           

          public class AddPointPhaseListener {

              @SuppressWarnings("deprecation")

              public void excute(Map paramMap,FacesContext facesContext) {

                  try {

                      //get form element from view。獲得頁面上的表單對象

                      UIComponent form = facesContext.getViewRoot().findComponent((String) paramMap.get("mapForm"));

                      if(form == null){

                          return;

                      }

           

                      //get map control。獲得mapControl

                      MapControl mc = (MapControl) form.findComponent((String) paramMap.get("mapId"));

                      if(mc == null){

                          return;

                      }

           

           

                      //get web map, web context & clear graphics。獲得WebMap,WebContext,就可以進行地圖操作了

                      WebMap wm = mc.getWebMap();

                      WebContext wc = wm.getWebContext();

                      wc.getWebQuery().clearGraphics();

                      

                      // read information form string of js function

                      //獲得從頁面javasctipt方法傳遞來的參數列表

                      String str=java.net.URLDecoder.decode((String) paramMap.get("str"), "UTF-8");

                      ///////////////////////

                      //如果需要返回的話,像這樣用XML的形式返回數據

                      //create and populate xml response document

                      Document doc = XMLUtil.newDocument();

                      Element responseTag = XMLUtil.createElement(doc, "response", null, null);

                      responseTag.setAttribute("test", "hello");

                      //write response

                      AJAXUtil.writeResponse(facesContext, doc);

                      

                  }

                  catch (Exception e) {

                  }

                  finally {

                      facesContext.responseComplete();

                  }

              }

          }



          5.最后,需要在faces-context.xml文件中通過注冊生命周期的方式,來使以上這些代碼生效:

          <!-- MapViewer Phase Listener -->

          <lifecycle>

              <!-- 這是默認的listener -->

              <phase-listener>

                  com.esri.adf.web.templates.MapViewerPhaseListener

              </phase-listener>


              <!-- 這是先前自定義的Listener -->

              <phase-listener>

                  com.megait.gis.listener.SelfPhaseListener

              </phase-listener>

          </lifecycle>
          posted on 2009-11-25 15:58 棋劍小秋 閱讀(1342) 評論(0)  編輯  收藏 所屬分類: GIS
          主站蜘蛛池模板: 庆阳市| 通城县| 林周县| 鄄城县| 太康县| 秦皇岛市| 泽库县| 丰宁| 芜湖市| 荔波县| 五峰| 万源市| 衡水市| 沭阳县| 黄山市| 鹰潭市| 浙江省| 平潭县| 周口市| 长沙县| 扶绥县| 高青县| 奉贤区| 出国| 攀枝花市| 华容县| 芜湖县| 尼玛县| 谷城县| 长宁区| 香港 | 外汇| 左贡县| 兴业县| 永康市| 达日县| 星子县| 冕宁县| 桃园县| 固镇县| 上高县|