`
iloveoracle
  • 浏览: 115350 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

dwr入门

    博客分类:
  • java
阅读更多
     
//官网参考地址:http://directwebremoting.org/dwr/index.html
 
<h1 style="font-size: 150%;">direct web remoting</h1>dwr is a java library that enables java on the server and javascript in a browser to interact and call each other as simply as possible.
dwr is easy ajax for java
 
 
dwr will generate the javascript to allow web browsers to securely call into java code almost as if it was running locally. it can marshal virtually any data including collections, pojos, xml and binary data like images and pdf files. all that is required is a security policy that defines what is allowed.
with reverse ajax, dwr allows java code running on a server to use client side apis to publish updates to arbitrary groups of browsers. this allows interaction 2 ways - browser calling server and server calling browser. dwr supports comet, polling and piggyback (sending data in with normal requests) as ways to publish to browsers.
dwr provides integration with spring, struts, guice, hibernate and others.
 
 
dwr is a rpc library which makes it easy to call java functions fromjavascript and to call javascript functions from java (a.k.a reverseajax).
 
 
dwr consists of two main parts:
  • a java servlet running on the server that processes requests andsends responses back to the browser.
  • javascript running in the browser that sends requests and candynamically update the webpage.
dwr works by dynamically generating javascript based on javaclasses. the code does some ajax magic to make it feel like theexecution is happening on the browser, but in reality the server isexecuting the code and dwr is marshalling the data back and forwards.
 
this method of remoting functions from java to javascript gives dwrusers a feel much like conventional rpc mechanisms like rmi or soap,with the benefit that it runs over the web without requiringweb-browser plug-ins.
 
<pre><create creator="new" javascript="jdate">      <param name="class" value="java.util.date"/>    </create></pre> 
the new creator that we used above uses the public no-args constructor that all javabeans must have. it is also worth remembering that dwr has one restriction:
  • 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 in javascript but not java.
visit the dwr.xml documentation for a detailed description of the dwr.xml file, creators and converters.
 
the javascript attribute is required - it gives your newly created object a name in the browser. you should avoid using javascript reserved words.
the scope attribute is largely the same as the scope attribute as defined by the servlet spec. it allows you to specify what a bean is available to. the options are "application", "session", "request", "page" and an additional scope "script". the first 4 of these values should be familiar to servlet and jsp developers. "script" scope allows you to have something similar to an http session that is tied to an id in a page rather than in a cookie.
the scope attribute is optional. it defaults to "page".
 
dwr comes with some small javascript libraries to help you:
  • engine.js: handles allserver communication
  • util.js: helps you alterweb pages with the data you got from the server (and a few neat extrastoo)
//配置(注意版本)
http://directwebremoting.org/dwr/introduction/getting-started.html
//http://directwebremoting.org/dwr/documentation/browser/util/addoptions.html
//http://directwebremoting.org/dwr/documentation/browser/util/lists.html
//例子
下载2.0版本
根据下载的版本配置,也可根据上面来配置,但官网上配置的版本是3.0的,所以dwr.xml需从2.0中得到dtd。
 
重点是下载中的例子
在eclipse中运行项目,找出下面的内容(其中1、3是要掌握的,2一般少用):
tutorial style examples in this war file:
  • dynamic text: a very simple documented example that updates a page with some text.
  • resource forwarding: how to dynamically include the contents of one page in another.
  • editable table: another simple documented example that allows the user to edit a table of data.
以上的例子需理解其中的源码,尤其是编辑表格。
<script id="gtbtranslateelementcode">var gtbtranslateonelementloaded;(function(){var lib = null;var checkreadycount = 0;function sendmessage(message, attrs) {  var data = document.getelementbyid(&amp;quot;gtbtranslateelementcode&amp;quot;);  for (var p in attrs) {    data.removeattribute(p);  }  for (var p in attrs) {    if (&amp;quot;undefined&amp;quot; != typeof attrs[p]) {      data.setattribute(p, attrs[p]);    }  }  var evt = document.createevent(&amp;quot;events&amp;quot;);  evt.initevent(message, true, false);  document.dispatchevent(evt);}function checklibready (){  var ready = lib.isavailable();  if (ready) {    sendmessage(&amp;quot;gtbtranslatelibready&amp;quot;, {&amp;quot;gtbtranslateerror&amp;quot; : false});    return;  }  if (checkreadycount++ &amp;gt; 5) {    sendmessage(&amp;quot;gtbtranslatelibready&amp;quot;, {&amp;quot;gtbtranslateerror&amp;quot; : true});    return;  }  settimeout(checklibready, 100);}gtbtranslateonelementloaded = function () {  lib = google.translate.translateservice({});  sendmessage(&amp;quot;{evt_loaded}&amp;quot;, {}, []);  var data = document.getelementbyid(&amp;quot;gtbtranslateelementcode&amp;quot;);  data.addeventlistener(&amp;quot;gtbtranslate&amp;quot;, ontranslaterequest, true);  data.addeventlistener(&amp;quot;gtbtranslatecheckready&amp;quot;, oncheckready, true);  data.addeventlistener(&amp;quot;gtbtranslaterevert&amp;quot;, onrevert, true);  checklibready();};function oncheckready() {  var ready = lib.isavailable();  sendmessage(&amp;quot;gtbtranslatelibready&amp;quot;, {&amp;quot;gtbtranslateerror&amp;quot; : !ready});}function ontranslaterequest() {  var data = document.getelementbyid(&amp;quot;gtbtranslateelementcode&amp;quot;);  var orig = data.getattribute(&amp;quot;gtboriginallang&amp;quot;);  var target = data.getattribute(&amp;quot;gtbtargetlang&amp;quot;);  lib.translatepage(orig, target, onprogress);}function onprogress(progress, opt_finished, opt_error) {  sendmessage(&amp;quot;gtbtranslateonprogress&amp;quot;, {&amp;quot;gtbtranslateprogress&amp;quot; : progress,       &amp;quot;gtbtranslatefinished&amp;quot; : opt_finished, &amp;quot;gtbtranslateerror&amp;quot; : opt_error});}function onrevert() {  lib.restore();}})(); (function(){var d=window,e=document;function f(b){var a=e.getelementsbytagname(&amp;quot;head&amp;quot;)[0];a||(a=e.body.parentnode.appendchild(e.createelement(&amp;quot;head&amp;quot;)));a.appendchild(b)}function _loadjs(b){var a=e.createelement(&amp;quot;script&amp;quot;);a.type=&amp;quot;text/javascript&amp;quot;;a.charset=&amp;quot;utf-8&amp;quot;;a.src=b;f(a)}function _loadcss(b){var a=e.createelement(&amp;quot;link&amp;quot;);a.type=&amp;quot;text/css&amp;quot;;a.rel=&amp;quot;stylesheet&amp;quot;;a.charset=&amp;quot;utf-8&amp;quot;;a.href=b;f(a)}function _isns(b){for(var b=b.split(&amp;quot;.&amp;quot;),a=d,c=0;c&amp;lt;b.length;++c)if(!(a=a[b[c]]))return!1;return!0}function _setupns(b){for(var b=b.split(&amp;quot;.&amp;quot;),a=d,c=0;c&amp;lt;b.length;++c)a=a[b[c]]||(a[b[c]]={});return a}d.addeventlistener&amp;amp;&amp;amp;typeof e.readystate==&amp;quot;undefined&amp;quot;&amp;amp;&amp;amp;d.addeventlistener(&amp;quot;domcontentloaded&amp;quot;,function(){e.readystate=&amp;quot;complete&amp;quot;},!1);if (_isns('google.translate.element')){return}var c=_setupns('google.translate._const');c._cl='zh-cn';c._cuc='gtbtranslateonelementloaded';c._cac='';c._cam='lib';var h='translate.googleapis.com';var b=(window.location.protocol=='https:'?'https://':'http://')+h;c._pah=h;c._pbi=b+'/translate_static/img/te_bk.gif';c._pci=b+'/translate_static/img/te_ctrl3.gif';c._phf=h+'/translate_static/js/element/hrs.swf';c._pli=b+'/translate_static/img/loading.gif';c._plla=h+'/translate_a/l';c._pmi=b+'/translate_static/img/mini_google.png';c._ps=b+'/translate_static/css/translateelement.css';c._puh='translate.google.com';_loadcss(c._ps);_loadjs(b+'/translate_static/js/element/main_zh-cn.js');})();</script>
 
0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics