<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ù)說明:
<init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param>
如果設(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ù)器端方法大功告成。