2008 Come on as you are~
ExtJs 在Web UI已经获得很大的殊荣,但单凭借它 Client UI 还无法霸占BS 那么丰富的应用。。他还要选择 一个Web层与后台一起来交互完成一个完美的系统。
Extjs的天生丽质的, 完美了融合JQuery,Prototype,YUI, 和她结合当然也需要出类拔萃,生出名门。 如果采用Java为开发主语言,那么 ExtJS + DWR + SPRING 算是门当户对。
可惜Extjs 的DataStore 的 prxy 并没有DWR一席,因为DWR返回的是Java对象与JSON非常像, 然而DWR 的在于它与Web远程Java方法的直接会话,因此有着非常灵活的一面。所以,有时候使用DWR对于J2EE来说,要更好于JSON,他做了JSON做不到事。
extjs官方论坛里已经有人编写了这样的扩展,后经人丰富,现在基本能满足要求了。
DWR扩展代码如下,创建 Ext.data.DWRProxy类。同样也继承Ext.data.DataProxy ,拿来show着解读一下。
dwr.js
Ext.data.DWRProxy = function(dwrCall, pagingAndSort){
Ext.data.DWRProxy.superclass.constructor.call(this);
this.dwrCall = dwrCall;
//this.args = args;
this.pagingAndSort = (pagingAndSort!=undefined ? pagingAndSort : true);
};
Ext.extend(Ext.data.DWRProxy, Ext.data.DataProxy, {
load : function(params, reader, callback, scope, arg) {
if(this.fireEvent(”beforeload”, this, params) !== false) {
var sort;
if(params.sort && params.dir) sort = params.sort + ‘ ‘ + params.dir;
else sort = ”;
var delegate = this.loadResponse.createDelegate(this, [reader, callback, scope, arg], 1);
var callParams = new Array();
if(arg.arg) {
callParams = arg.arg.slice();
}
if(this.pagingAndSort) {
callParams.push(params.start);
callParams.push(params.limit);
callParams.push(sort);
}
callParams.push(delegate);
this.dwrCall.apply(this, callParams);
} else {
callback.call(scope || this, null, arg, false);
}
},
loadResponse : function(listRange, reader, callback, scope, arg) {
var result;
try {
result = reader.read(listRange);
} catch(e) {
this.fireEvent(”loadexception”, this, null, response, e);
callback.call(scope, null, arg, false);
return;
}
callback.call(scope, result, arg, true);
},
update : function(dataSet){},
updateResponse : function(dataSet)
{}
});
Ext.data.ListRangeReader = function(meta, recordType){
Ext.data.ListRangeReader.superclass.constructor.call(this, meta, recordType);
this.recordType = recordType;
};
Ext.extend(Ext.data.ListRangeReader, Ext.data.DataReader, {
getJsonAccessor: function(){
var re = /[\[\.]/;
return function(expr) {
try {
return(re.test(expr))
? new Function(”obj”, “return obj.” + expr)
: function(obj){
return obj[expr];
};
} catch(e){}
return Ext.emptyFn;
};
}(),
read : function(o){
var recordType = this.recordType, fields = recordType.prototype.fields;
//Generate extraction functions for the totalProperty, the root, the id, and for each field
if (!this.ef) {
if(this.meta.totalProperty) {
this.getTotal = this.getJsonAccessor(this.meta.totalProperty);
}
if(this.meta.successProperty) {
this.getSuccess = this.getJsonAccessor(this.meta.successProperty);
}
if (this.meta.id) {
var g = this.getJsonAccessor(this.meta.id);
this.getId = function(rec) {
var r = g(rec);
return (r === undefined || r === “”) ? null : r;
};
} else {
this.getId = function(){return null;};
}
this.ef = [];
for(var i = 0; i < fields.length; i++){
f = fields.items[i];
var map = (f.mapping !== undefined && f.mapping !== null) ? f.mapping : f.name;
this.ef[i] = this.getJsonAccessor(map);
}
}
var records = [];
var root = o.data, c = root.length, totalRecords = c, success = true;
if(this.meta.totalProperty){
var v = parseInt(this.getTotal(o), 10);
if(!isNaN(v)){
totalRecords = v;
}
}
if(this.meta.successProperty){
var v = this.getSuccess(o);
if(v === false || v === ‘false’){
success = false;
}
}
for(var i = 0; i < c; i++){
var n = root[i];
var values = {};
var id = this.getId(n);
for(var j = 0; j < fields.length; j++){
f = fields.items[j];
var v = this.ef[j](n);
values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue);
}
var record = new recordType(values, id);
records[i] = record;
}
return {
success : success,
records : records,
totalRecords : totalRecords
};
}
});
经过改造之后,那么DWR可以像JSON一样,返回的数据作为DataStore
代码片断
ds = new Ext.data.Store({ proxy: new Ext.data.DWRProxy(TodoService.getItems, true), //TotoService.getItems dwr开放出来的Java获取数据方法 reader: new Ext.data.ListRangeReader( {id:'id', totalProperty:'totalSize'}, recordType), remoteSort: true });
Spring实现了强大的管理了后台的JavaBean,干干净净的注入创建了每个对象, DWR天生又是支持Spring,无缝的讲Spring的后台Bean 发布到Web层作为JavaScript对象,和JavaScript方法。中途不需要人工干预写Servlet.
不多说了, 跑了下代码,通过,可以直接下载。
download/ext-dwr-spring.rar
偶的Blog 不结群,不跟风,如有雷同,存属巧合~
主要目的是请诸位相信:这个世界上,巧合的事情很多....
Allen Lee X
October 23rd, 2007 at 10:02 am
不知道grid.js里面哪里有问题,在我机器上出现如下的js错误:
Line:35
Char:6
Code:0
Error:缺少标识符、字符串或数字
Troy
November 7th, 2007 at 9:49 am
把你的webcontent目录部署到tomcat的webapps目录就可以了么?访问的url是什么?新手请求指教
more
December 5th, 2007 at 4:15 pm
greatings…
exellent…
Software Development Guide
December 7th, 2007 at 8:19 am
Software Development Guide…
I couldn’t understand some parts of this article, but it sounds interesting…
Marguerite
April 7th, 2008 at 3:01 am
The only thing I figured out, is that you will have a headache after reading this unreal stuff. Nothing interesting!
Darren
April 9th, 2008 at 6:56 pm
Risky theme. I think you’ve hurt someone’s feelings, but what’s for me - I like it. No matter what they say if your opinion is true.
Jet
April 14th, 2008 at 3:15 pm
为什么不能下载了呢?
order soma online
June 25th, 2008 at 6:36 am
soma…
define soma…
azulfidine side effects and erection in men
June 28th, 2008 at 1:06 pm
azulfidine…
azulfidine pricing…
servlet scope
June 30th, 2008 at 3:10 am
servlet scope…
For very small rigid borescopes, the gradient index lens relays are better. Relay optics in rigid borescopes can be of 3 basic types,…
cialis price
July 7th, 2008 at 1:55 am
cialis…
cialis forum…
dehydration and lasix
July 19th, 2008 at 1:12 am
lasix…
drug interactions with lasix…
fosamax more medical authorities
July 23rd, 2008 at 9:33 pm
fosamax…
fosamax 10mg…
soma
August 2nd, 2008 at 6:55 pm
soma…
soma watson next day no rx required…
tramadol
August 2nd, 2008 at 6:55 pm
tramadol…
tramadol pics…
soma
August 2nd, 2008 at 6:55 pm
soma…
hatsuharu soma…
tramadol
August 2nd, 2008 at 6:55 pm
tramadol…
tramadol addiction…
tramadol
August 2nd, 2008 at 6:55 pm
tramadol…
tramadol mpllc…
tramadol
August 2nd, 2008 at 6:55 pm
tramadol…
tramadol drug…
tramadol
August 2nd, 2008 at 6:55 pm
tramadol…
50 mg tramadol…
FancyYork
August 7th, 2008 at 6:31 am
[URL=http://cheapdownload.org/info-Ahead_NeroMix_1.4.html]Ahead NeroMix 1.4 software[/URL]
231sd
August 16th, 2008 at 1:51 am
8dBmh9 dfkiuwkjfd kldfij fff
FancyYork
August 20th, 2008 at 6:55 am
[URL=http://cheapdownload.org/info-Cakewalk_Sonar_4_Producer_Edition.html]Cakewalk Sonar 4 Producer Edition software[/URL]
qeliwgnbc ybagkq
August 25th, 2008 at 10:34 am
brpeym lcyadvf nzyl vpfzasd nmgoky vuczhal xzgek
FancyYork
September 2nd, 2008 at 10:36 pm
[URL=http://cheapdownload.org/info-Micro
soft_PhotoDraw_2.0.html]Microsoft PhotoDraw 2.0 software[/URL]
zithromax
September 7th, 2008 at 12:35 pm
zithromax…
soma online. soma carisoprodol. akane soma. cheap soma. cheap soma. soma cruz. cheap soma. buy soma. cheap soma. watson soma. cheap tramadol. tramadol hcl. tramadol hydrochloride. buy tramadol. soma online. cheap soma. buy soma online. soma. generic so…