java學(xué)習(xí)

          java學(xué)習(xí)

           

          extjs中directjngine的配置

          第一步,在web.xml中配置DirectJNgine Servlet.我配置的web.xml如下:
          在web.xml配置文件中寫:
              <?xml version="1.0" encoding="UTF-8"?>  
              
          <web-app version="2.5"   
                  xmlns
          ="http://java.sun.com/xml/ns/javaee"   
                  xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"   
                  xsi:schemaLocation
          ="http://java.sun.com/xml/ns/javaee   
                  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
              <!-- 以下為DirectJNgine servlet 默認(rèn)配置-->  
                
          <servlet>  
                  
          <servlet-name>DjnServlet</servlet-name>  
                  
          <servlet-class>com.softwarementors.extjs.djn.servlet.DirectJNgineServlet</servlet-class>  
                   
                  
          <init-param>  
                    
          <param-name>debug</param-name>  
                    
          <param-value>true</param-value>  
                  
          </init-param>    
                
                  
          <init-param>  
                    
          <param-name>providersUrl</param-name>  
                    
          <param-value>djn/directprovider</param-value>  
                  
          </init-param>  
              
          <!-- DirectJNgine servlet 默認(rèn)配置-->  
                
              
          <!-- api域:對(duì)應(yīng)下面的各個(gè)param-name的前綴,可以設(shè)置多個(gè)不同的域-->  
                  
          <init-param>  
                    
          <param-name>apis</param-name>  
                    
          <param-value>  
                      mynamespace  
                    
          </param-value>  
                  
          </init-param>  
                
              
          <!-- mynamespace對(duì)應(yīng)上面的api域。MyAction對(duì)應(yīng)生成的js相應(yīng)的文件夾.服務(wù)器運(yùn)行后,將在MyAction/下存放自動(dòng)生成的3個(gè)js文件。這里的名稱分別為  
                  MyActionApi.js,MyActionApi
          -debug.js,MyActionApi-min.js  
               
          -->   
                  
          <init-param>  
                    
          <param-name>mynamespace.apiFile</param-name>  
                    
          <param-value>MyAction/MyActionApi.js</param-value>  
                  
          </init-param>  
              
          <!-- mynamespace.對(duì)應(yīng)上面的域."Ext.zhouyang"為命名空間所對(duì)應(yīng)的值。會(huì)在頁面加載時(shí)候用上. -->  
                  
          <init-param>  
                    
          <param-name>mynamespace.apiNamespace</param-name>  
                    
          <param-value>Ext.zhouyang</param-value>  
                  
          </init-param>  
              
          <!-- mynamespace.對(duì)應(yīng)上面的域. 要使用的類的全路徑名 -->  
                  
          <init-param>  
                    
          <param-name>mynamespace.classes</param-name>  
                    
          <param-value>  
                    com.softwarementors.extjs.djn.MyAction.MyAction  
                    
          </param-value>  
                  
          </init-param>  
                    
                  
          <load-on-startup>1</load-on-startup>  
                
          </servlet>   
                 
                
          <servlet-mapping>  
                  
          <servlet-name>DjnServlet</servlet-name>  
                  
          <url-pattern>/djn/directprovider/*</url-pattern>  
                </servlet-mapping>  
                  
                <welcome-file-list>  
                  <welcome-file>  
                    index.html  
                  </welcome-file>  
                </welcome-file-list>  
              </web-app>  

          第二步,使你的服務(wù)器端方法對(duì)JavaScript可見,其實(shí)就是說對(duì)客戶端可見。

          以我的demo為例,我想在hello.html中調(diào)用服務(wù)器端的方法。于是,我在hello.html中添加如下一段話。

            <!--以下為DirectJNgine的基礎(chǔ)JS文件,放在djn目錄下,直接引用就可以。-->
            
          <script type="text/javascript" src="../djn/djn-remote-call-support.js"></script>
            
          <script type="text/javascript" src="../ejn/ejn-assert.js"></script>
            
          <!--以上為DirectJNgine的基礎(chǔ)JS文件.-->
            
          <script type="text/javascript" src="MyActionApi.js"></script>

          前兩個(gè)script引用,是用來調(diào)用directjngine提供的默認(rèn)的一些操作函數(shù)。只需引用即可,不需要關(guān)注太多。

          最后一個(gè)js,在啟動(dòng)服務(wù)器前,你是看不到的。因?yàn)樗莇irectjngine項(xiàng)目,根據(jù)你的配置自動(dòng)生成的。至于其中到底是怎樣,下面我會(huì)詳細(xì)介紹。

           

          第三步,設(shè)計(jì)服務(wù)器端的方法。如函數(shù)名稱,是否需要返回值等等。因?yàn)樵趆ello.html頁面,我將會(huì)調(diào)用方法。

          具體調(diào)用代碼將在最后的hello.html代碼說明部分進(jìn)行集中說明。

           

          第四步,使用Java語言,編寫服務(wù)器端的方法。附上代碼如下:

          1. package com.softwarementors.extjs.djn.MyAction;  
          2. import com.softwarementors.extjs.djn.config.annotations.DirectMethod;  
          3. public class MyAction {  
          4.     @DirectMethod  
          5.     public String doEcho(String parm)  
          6.     {  
          7.         return "參數(shù)是" + parm;  
          8.     }  
          9.     @DirectMethod  
          10.     public String doShow()  
          11.     {  
          12.         return "i am not easy";  
          13.     }  

          注意:

          @DirectMethod這個(gè)標(biāo)簽很重要,就是通過這個(gè)標(biāo)簽和web.xml的一些配置,才會(huì)動(dòng)態(tài)生成第二步中需要引入的js。這種書寫方式下, 你在Java代碼中書寫的方法名稱就是你在前臺(tái)JS調(diào)用的方法名稱。如果你覺得這樣子不安全,或是不專業(yè)。那么可以通過定義別名的方法對(duì)方法進(jìn)行訪問。書 寫形式如下:

          @DirectMethod( method="nameyouwant")

          這樣子定義之后,前臺(tái)JS調(diào)用服務(wù)器端方法時(shí),方法的名稱就是你紅色定義的名稱了。

          把類編譯好后,把class文件放入WEB-INF\classes相應(yīng)的包目錄下。要與web.xml中class文件的包的目錄結(jié)構(gòu)相同。

           

          第五步,告訴DirectJNgine 去哪里尋找服務(wù)器端的方法。

          在web.xml的配置中,有如下代碼:

              <init-param>
                <param-name>mynamespace.classes</param-name>
                <param-value>
                com.softwarementors.extjs.djn.MyAction.MyAction
                </param-value>
              </init-param>

          在這里需要注意,mynamespace.classes的紅色部分,一定要與web.xml上面的apis變量的mynamespace相同。

          其次,com.softwarementors.extjs.djn.MyAction.MyAction 為第四步中書寫的類的全路徑名稱,如果有多個(gè)類,則用英文逗號(hào)分隔。

           

          第六步,為了讓Extjs可以調(diào)用我們的服務(wù)器端方法,我們需要注冊一個(gè)遠(yuǎn)程調(diào)用提供者即a remoting provider。你需要做的就是在hello.html代碼中,加入如下語句,為某一個(gè)空間注冊一個(gè)遠(yuǎn)程調(diào)用提供者:

          //此處通過命名空間,添加初始化遠(yuǎn)程調(diào)用API
          Ext.zhouyang.REMOTING_API.enableBuffer = 0;
          Ext.Direct.addProvider(Ext.zhouyang.REMOTING_API);

          注意:上面的Ext.zhouyang為在web.xml變量中mynamespace.apiNamespace已經(jīng)定義。

           

          第七步,通過JavaScript進(jìn)行調(diào)用服務(wù)器端的方法。

          MyAction.doShow(function(result, e){
              var t = e.getTransaction();
              if(e.status){
                  out.append(
                                     String.format('<p><b>Successful call to {0}.{1} with response:</b><xmp>{2}</xmp></p>',
                         t.action,
                                     t.method,
                                     Ext.encode(result)));
              }else{
                  out.append(
                                     String.format('<p><b>Call to {0}.{1} failed with message:</b><xmp>{2}</xmp></p>',
                         t.action,
                                     t.method,
                                     e.message));
                  }
                  out.el.scroll('b', 100000, true);
              });        
              
              //doEcho函數(shù),此函數(shù)有參數(shù)。
              var parm = txtparm.value;  //要傳入后臺(tái)的參數(shù)
              MyAction.doEcho(parm, function(result, e){
                  var t = e.getTransaction();
                  if(e.status){
                      out.append(String.format('<p><b>Successful call to {0}.{1} with response:</b><xmp>{2}</xmp></p>',
                          t.action, t.method, Ext.encode(result)));
                  }else{
                      out.append(String.format('<p><b>Call to {0}.{1} failed with message:</b><xmp>{2}</xmp></p>',
                          t.action, t.method, e.message));
                  }
                  out.el.scroll('b', 100000, true);
              });                       

          上面的代碼排版有點(diǎn)亂,這里先做些說明,這個(gè)demo的下載網(wǎng)址,我最后會(huì)附送??梢灾苯硬榭创a。

           

          可以看到,對(duì)于函數(shù)的結(jié)構(gòu)。如果需要傳入?yún)?shù),則把參數(shù)寫在函數(shù)前面。

          因?yàn)镴avaScript調(diào)用服務(wù)器端方法是異步的,所以,最好的方法就是定義一個(gè)回調(diào)函數(shù)處理數(shù)據(jù),而不是讓程序終止。

          所以,對(duì)于上面的兩個(gè)方法,最后都定義了一個(gè)回調(diào)函數(shù)。這個(gè)函數(shù)的作用是用來處理服務(wù)器端返回的數(shù)據(jù)。

          參數(shù)result是服務(wù)器端方法返回的數(shù)據(jù),e是一個(gè)even對(duì)象,包括一個(gè)事務(wù)對(duì)象,這個(gè)對(duì)象包含action和method的名稱和其他一些信息。

           

          e.status表示服務(wù)器端成功執(zhí)行了函數(shù)。如果返回false,則表示服務(wù)器端出現(xiàn)了錯(cuò)誤。通過e.message就可以查看出錯(cuò)信息。

           

          其他參數(shù)說明:

        1. <init-param>  
        2.   <param-name>debug</param-name>  
        3.   <param-value>true</param-value>  
        4. </init-param>

        5. 如果設(shè)置為true,在tomcat的log目錄下的stdout_2010****.log文件中會(huì)輸入相關(guān)的打印信息。如:
          INFO : com.softwarementors.extjs.djn.servlet.DirectJNgineServlet - "Servlet GLOBAL configuration: debug=true, providersUrl=djn/directprovider, minify=true, batchRequestsMultithreadingEnabled=true, batchRequestsMinThreadsPoolSize=16, batchRequestsMaxThreadsPoolSize=80, batchRequestsMaxThreadsPerRequest=8, batchRequestsMaxThreadKeepAliveSeconds=60, gsonBuilderConfiguratorClass=com.softwarementors.extjs.djn.gson.DefaultGsonBuilderConfigurator, dispatcherClass=com.softwarementors.extjs.djn.servlet.ssm.SsmDispatcher, jsonRequestProcessorThreadClass=com.softwarementors.extjs.djn.servlet.ssm.SsmJsonRequestProcessorThread, contextPath=--not specified: calculated via Javascript--, createSourceFiles=true" ()
          INFO : com.softwarementors.extjs.djn.servlet.DirectJNgineServlet - "Servlet GLOBAL configuration: registryConfiguratorClass=" ()
          INFO : com.softwarementors.extjs.djn.servlet.DirectJNgineServlet - "Servlet APIs configuration: apis=mynamespace" ()
          INFO : com.softwarementors.extjs.djn.servlet.DirectJNgineServlet - "Servlet 'mynamespace' Api configuration: apiNamespace=Ext.zhouyang, actionsNamespace=, apiFile=MyAction/MyActionApi.js => Full api file: C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\directdemo\MyAction\MyActionApi.js, classes=com.softwarementors.extjs.djn.MyAction.MyAction" ()
          INFO : com.softwarementors.extjs.djn.jscodegen.CodeFileGenerator - "Creating source files for APIs..." ()

          如果非調(diào)試狀態(tài),則可以置為false。

           

          完成上面的步驟后,啟動(dòng)tomcat,發(fā)現(xiàn)在\Tomcat 6.0\webapps\directdemo\MyAction 目錄下生成了三個(gè)文件。

          如下:

          MyActionApi.js,MyActionApi-debug.js,MyActionApi-min.js。其中的MyActionApi.js就是我們在第二步中引入的JavaScript

          它的作用相當(dāng)于Server端代碼的API一樣,因?yàn)橛兴拇嬖?,客戶端的網(wǎng)頁才知道服務(wù)器端都定義了些什么方法。我的demo中,生成的MyActionApi.js的代碼如下:

          /**********************************************************************
           *
           * Code generated automatically by DirectJNgine
           * Copyright (c) 2009, Pedro Agulló Soliveres
           *
           * DO NOT MODIFY MANUALLY!!
           *
           **********************************************************************/

          Ext.namespace( 'Ext.zhouyang');

          Ext.zhouyang.PROVIDER_BASE_URL=window.location.protocol + '//' + window.location.host + '/' + (window.location.pathname.split('/').length>2 ? window.location.pathname.split('/')[1]+ '/' : '')  + 'djn/directprovider';

          Ext.zhouyang.POLLING_URLS = {
          }

          Ext.zhouyang.REMOTING_API = {
            url: Ext.zhouyang.PROVIDER_BASE_URL,
            type: 'remoting',
            actions: {
              MyAction: [
                {
                  name: 'doEcho'/*(String) => String */,
                  len: 1,
                  formHandler: false
                },
                {
                  name: 'doShow'/*() => String */,
                  len: 0,
                  formHandler: false
                }
              ]
            }
          }
          可以看到,包括函數(shù)名稱,參數(shù)類型,參數(shù)個(gè)數(shù)等都有定義。

           至此,directjngine、Ext Direct調(diào)用Java服務(wù)器端方法大功告成。


          posted on 2013-01-05 10:18 楊軍威 閱讀(800) 評(píng)論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 永济市| 新和县| 稷山县| 平顶山市| 三原县| 松溪县| 扎兰屯市| 和田县| 饶平县| 巴彦县| 广州市| 密云县| 汉川市| 留坝县| 鹤岗市| 宁津县| 黄冈市| 汤原县| 左云县| 乌拉特中旗| 宣恩县| 武宁县| 手机| 冕宁县| 五河县| 正定县| 绥宁县| 青田县| 苍溪县| 福州市| 开江县| 革吉县| 胶州市| 都江堰市| 丰县| 许昌市| 松潘县| 西城区| 隆化县| 镇江市| 铜陵市|