読み物小舖 -- DWR
AJAX, 最近很多人都在討論 ... 拜Google所賜, 這幾年吵到翻.
根據梁大師預測, 這東西一定會成為主流, 既然梁公都這麼說了, 小子我輩等怎可不仔細研讀呢 ... 拜讀拜讀
DWR: Easy AJAX for JAVA
AJAX的定義是啥? 全名叫做Asynchronized Javascript and Xml, 最佳代言人即是Google系列產品.
DWR, 是由Getahead出品, 這家公司(或組織?)也很有趣, 他們說自己是
Getahead is a small family run IT consultancy.
Joe Walker - Java/J2EE Architecture and Development
Mark Goodwin - Java/Embedded/Security Development
是的, 兩個人 ... 嚇死人, 兩個人就可以搞出這些名堂, 但是我覺得是假名字, 怎麼一個叫做"喬步行者", 一個叫做"馬克好贏" ???
AJAX到底有多紅, Getahead的比喻真正好 -- AJAX is the buzz-word of the moment to web developers. (最近這名詞真的嗡嗡叫到一個程度, 覺得耳朵有點癢了)
The problem for the web developer is that while this is a very attractive way of creating web-sites,
and one where you can get started without a huge amount of effort,
there are a number of pitfalls which can make life harder for them.
All of the browsers have different quirks, so you can easily discover that you have
locked Mac users out of the party.
對Web開發者而言, 問題在於當這已經是一個非常吸引人的建立網站技術, 而且你可以不用花太多力氣就可以開始, 還是會有一些讓你生活變困難的圈套存在.
所有的瀏覽器都有不同的警語, 所以你可以輕易的發現, 你已經把MAC使用者趕出派對了(用MAC的人站起來阿...)
DWR consists of two main parts: JavaScript running in the user's browser to communicate with the server
and dynamically update the webpage, and a Java Servlet running on the server that processes requests and
sends responses back to the browser.
DWR和大多支援AJAX的framework一樣, 透過JavaScript作為瀏覽器與伺服器之間的橋樑, 包含更新網頁上的內容等. J2EE的Servlet, 則是伺服器端處理request與response的機制.
DWR is a Java open source library which helps developers wanting to write web sites that include AJAX technology.
It allows code in a web browser to use Java functions running on a web server as if it was in the browser.
DWR consists of two main parts: JavaScript running in the user's browser to communicate with the server and dynamically update the webpage, and a Java Servlet running on the server that processes requests and sends responses back to the browser.
DWR takes a novel approach to AJAX by dynamically generating JavaScript code based Java classes.
DWR這東西可以幫助工程師寫出(做出?)內含AJAX技術的網站.
利用Java code動態產生Javascript, 因此才能利用JavaScript使用JAVA的功能, 就好像你只是操作普通HTML網頁一樣(這段我翻的很爛).
早期AJAX常為人詬病的就是安全性, 這邊特別提醒大家For security reasons the web developer must configure exactly which Java classes are safe to export. 可千萬別以為會寫程式就好, 也要注意這種東西阿.
這邊有張圖很有意思:
說明DWR是如何改變下拉式選單的內容, 就好像用JavaScript的onclick一樣. 仔細看左右兩邊可有相關聯的地方喔.
Deploy DWR真的很簡單, 下載jar檔然後設定一些web.xml和dwr.xml就可以了.
但是要注意三件事情:
1. dwr.xml內定義了在DWR內可以用Javascript來create和remote的class.
The DWR config file defines what classes DWR can create and remote for use by Javascript.
2. 避免使用Javascript保留字.
Avoid reserved JavaScript words; Methods named after reserved words are automatically excluded. Most JavaScript reserved words are also Java
reserved words, so you won't be having a method called "try()" anyway. However the most common gotcha is "delete()", which has special meaning from JavaScript but
not Java.
3. 避免overload methods.
Overloaded methods can be involved in a bit of a lottery as to which gets called, so avoid overloaded methods.
在有公開聲明使用DWR技術的網站:
JIRA and Confluence : 這我很訝異, 真的 ... 雖然聽說部門有裝起來這兩樣東西, 但目前還沒看到url在哪.
Demo Portal : www.claudehussenet.com , 很有意思的一個網站, 用Direct Web Remoting,Spring Framework and Hibernate架構而成, 其實這就是我想要做的, 正在研究中.
DWR官方網站上的範例, 很清楚明白的告訴我們DWR是如何透過JavaScript呼叫Java Class處理request與reponse, 然後再把結果丟回網頁上.
http://getahead.ltd.uk/dwr/examples/text
http://getahead.ltd.uk/dwr/examples/form