DWR 用xom,dom4j傳輸Document或者Element數(shù)據(jù)的小問題。
作者: ynmc原文地址:http://www.aygfsteel.com/ynmc/archive/2006/06/26/55149.html歡迎轉(zhuǎn)載,請保留出處.
在用DWR傳輸xml文檔(document)或者節(jié)點(Element)的時候,通過DWR Debug頁面從函數(shù)中直接取返回數(shù)據(jù)時,由于DWR對于Firefox的支持度比IE好的原因,在FireFox下面能正常運行,在IE下會報[Ojbect Error]。我用DWR1.1和DWR M2兩個版本都進行了測試,都會報出同樣的錯誤。
關(guān)于這個錯誤,我已經(jīng)和DWR的作者joe walker通過user-list聯(lián)系過,他表示由于時間的問題,在ie下面的確有一些bug沒有來得及處理。
同時,我又做了一些測試,發(fā)現(xiàn)其實這個問題確實如Joe后來回復(fù)的郵件所說,是個沒有什么意義的小問題。在IE下其實也是可以傳遞Document或者Element數(shù)據(jù)的,只是在DWR Debug頁面下,直接從函數(shù)中返回Document或者Element時,DWR彈出的顯示元素的對話框。這個時候DWR隱式的將Document或者Element對象轉(zhuǎn)換成了String,而DWR對FireFox支持的較好,沒有問題,而在IE中就會彈出Object Error錯誤。
所以對實際應(yīng)用來講,這個bug完全可以忽略,因為除了debug以外,在使用中沒有任何影響。
Joe 最后回復(fù)了郵件,也給出了處理辦法,他的意思是這種情況出現(xiàn)的比較少,意義不大(我就是聽他說了這句話之后發(fā)現(xiàn)了開始自己的理解有誤,看來多與人交流是非常重要的啊。)
下面是郵件原文:
From: Joe Walker <joseph.walker@...>
Subject: Re: Re: [new user]problem: can't send/return DOM object?
Newsgroups: gmane.comp.java.dwr.user
Date: 2006-06-26 08:32:37 GMT (1 day and 48 minutes ago)
Expires: This article expires on 2006-07-10
The DOM converter will convert from browser DOM objects to server side DOM objects, but it wasn't designed to convert from browser strings to SS DOM.
How do you parse a browser string into a browser DOM?
This can't be done without some cross-browser tweaks. Take a look at DWREngine._unserializeDocument(). It is a private method to DWREngine, so you should not use it directly, but you can copy it.
DWREngine._unserializeDocument = function(xml) {
? var dom;
? if (window.DOMParser) {
??? var parser = new DOMParser();
??? dom = parser.parseFromString(xml, "text/xml");
??? if (!dom.documentElement || dom.documentElement.tagName == "parsererror") {
????? var message = dom.documentElement.firstChild.data;
????? message += "\n" + dom.documentElement.firstChild.nextSibling.firstChild.data;
????? throw message;
??? }
??? return dom;
? }
? else if ( window.ActiveXObject) {
??? dom = DWREngine._newActiveXObject(DWREngine._DOMDocument);
??? dom.loadXML(xml);
??? // What happens on parse fail with IE?
??? return dom;
? }
? else {
??? var div = document.createElement ('div');
??? div.innerHTML = xml;
??? return div;
? }
};
There is bug in IE is that I mentioned in a previous post. The bug is that HTML nodes do not have the DOM properties to allow DWR to marshall them properly. This might be something that DWR can work around, but to date I have never seen the need to fix the bug. Sending the browser HTML DOM to the server isn't all that useful since the server gave it to the browser in the first place.
Joe.
目前的ajax框架中DWR的文檔是做的比較好的,這也有利于廣大的開發(fā)者。
DWR 官方站點:
http://getahead.ltd.uk/dwr/
DWR User-list(作者會經(jīng)常對在這里提出的問題進行回答,目前已經(jīng)有約5000條記錄):
http://news.gmane.org/gmane.comp.java.dwr.user
最后感謝Joe Walker,他不僅寫出了DWR,還非常熱心的回答了我和其它user的問題。
posted on 2006-06-26 14:27 Ynmc 閱讀(2324) 評論(3) 編輯 收藏 所屬分類: Ajax