Client
要配置遠程服務器端程序在客戶端可以訪問,需要配置一個包含了所有可以訪問訪問服務器端方法的對象。請求api.js將會得到需要的配置。
<script type="text/javascript" src="action/api.js"></script>
action必須對應配置在web.xml DispatcherSerlvet的url-pattern值。
api.js文件是在請求時被動態創建的,如果需要一個可讀性更好的可以請求api-debug.js
<script type="text/javascript" src="action/api-debug.js"></script>
在api配置引入後,需要添加該對象到Ext.Direct用方法addProvider。
//Ext JS 3
Ext.Direct.addProvider(Ext.app.REMOTING_API);
//Ext JS 4.x and Sencha Touch 2
Ext.direct.Manager.addProvider(Ext.app.REMOTING_API);
在上面配置完後遠程的方法就可以在JavaScript訪問了。
如果還有遠程輪詢方法,也可以同時在添加該對象到Ext.Direct在一個方法裏。
//Ext JS 3
Ext.Direct.addProvider(Ext.app.REMOTING_API, {
id : 'messageProvider',
type : 'polling',
url : Ext.app.POLLING_URLS.message
});
//Ext JS 4.x and Sencha Touch 2
Ext.direct.Manager.addProvider(Ext.app.REMOTING_API, {
type: 'polling',
url: Ext.app.POLLING_URLS.message
});
Parameters
api.js支持下面查詢參數
Request Parameter | Description | Default |
---|---|---|
apiNs | Namespace the remoting api variable will be created in | Ext.app |
actionNs | Namespace the action beans will be created in | Browser global scope |
remotingApiVar | Name of the remoting api variable | REMOTING_API |
pollingUrlsVar | Name of the variable which contains the polling URLs | POLLING_URLS |
group | Name of one or more api groups. Multiple groups separated with comma. Since 1.3.1: When group is an empty string api.js returns all methods that do not have a group attribute or do have a group attribute with an empty value |
|
fullRouterUrl | true or false. If true the URL property contains the full router URL | false |
baseRouterUrl(since 1.3.2) | Overrides the default behavior and sets the value of the url field in the REMOTING_API object.
/controller/api.js?baseRouterUrl=myrouterurl //Output Ext.app.REMOTING_API = { "url" : "myrouterurl/router", ... } |
例子:
<script type="text/javascript" src="action/api.js?apiNs=RemoteNs&actionNs=MyApp&remotingApiVar=REMOTEAPI&pollingUrlsVar=POLLURLS"></script>
這個例子將會創建一個RemoteNs.REMOTEAPI變量
//Ext JS 3
Ext.Direct.addProvider(RemoteNs.REMOTEAPI);
//Ext JS 4.x and Sencha Touch 2
Ext.direct.Manager.addProvider(RemoteNs.REMOTEAPI);
Actions是MyApp命名空間一部分。
MyApp.testAction.doEcho('test', function(result, e){...});
輪詢RULs是POLLURLS對象的一部分
//Ext JS 3
Ext.Direct.addProvider({
type: 'polling',
url: RemoteNs.POLLURLS.message
});
//Ext JS 4.x and Sencha Touch 2
Ext.direct.Manager.addProvider({
type: 'polling',
url: RemoteNs.POLLURLS.message
});
Group
分組
如果一個程序包含多個頁面並且JavaScript代碼不需要訪問所有服務器方法在每個頁面,可以將這些方法分組暴露,僅服務器方法子集到某個頁面。
例子:
有兩個服務器方法可以用,我們僅想在第一個頁面調用第一個頁面,在第二個頁面調用第二個方法。
首先添加group屬性到@ExtDirectMethod註釋
@Component
public class TestAction {
@ExtDirectMethod(group = "group1")
public long multiply(long num) {
...
}
@ExtDirectMethod(group = "group2")
public String doEcho(String message) {
...
}
}
一個方法可以是多個組的一部分。多個組名用‘,’分開 @ExtDirectMethod(group = "group3,group4,group5")。
在第一個頁面添加下面script片段。api.js將會只導入配置的mutiply方法。
<script type="text/javascript" src="action/api.js?group=group1"></script>
在另一個頁面我們添加如下script片段。這個例子導入了doEcho方法在這段代碼。
<script type="text/javascript" src="action/api.js?group=group2"></script>
查詢參數group可以請求多個組,下面例子導入了兩個組的方法。
<script type="text/javascript" src="action/api.js?group=group1,group2"></script>
Since 1.3.1:
查詢參數的值可以是空字符串。其它例子返回每個方法沒有group屬性在@ExtDirectMethod註釋或者有group屬性是空值的。
<script type="text/javascript" src="action/api.js?group="></script>
或沒有‘=’。
<script type="text/javascript" src="action/api.js?group"></script>
緩存(since 1.2.1)
當請求api.js添加版本號時將會添加如下HTTP頭到響應:
Vary
Expires
ETag
Cache-Control
這些HTTP頭將會強制客戶端緩存響應。一個指紋請求例子如下:
<script type="text/javascript" src="action/api-1.0.js"></script>
“API”這個詞連字號後,之後的任何字符例子如下。
api-a.js, api-1.1.1.js, api-ac12e5f914.js, api-20120808101545.js
例子:
請求 api.js
HTTP/1.1 200 OK
Date: Tue, 02 Oct 2012 06:06:22 GMT
Content-Type: application/javascript;charset=UTF-8
Vary: Accept-Encoding
請求 api-1.0.js
HTTP/1.1 200 OK
Date: Tue, 02 Oct 2012 06:05:40 GMT
Vary: Accept-Encoding
Expires: Sun, 31 Mar 2013 06:05:40 GMT
ETag: "04feffd452e5177c41d874defb0ae4a1c"
Cache-Control: public, max-age=15552000
Content-Type: application/javascript;charset=UTF-8