ajax框架dwr开发

dwr实现AJAX非常先进。比如动态生成javaScript代码;隐藏的HTTP协议,javascript用于java代码交互的javaScript对象。
 自从我开始DWR感觉DWR是用javaScript对象去代替java类的对象去做某些事情。
 比如一个java类名为:ShowHello  当你在dwr.xml文件做了映射 javascrip对象叫 showHello 那么你在html文件或jsp文件中想使用ShowHello中的某些方法。那就可直接使用showHello就可以了。dwr就是这样的功能。
 使用时要注意以下几点:
   1.引入两个包。dwr.jar,xalan.jar这个包一般的情况在高版中把这个jar包的类已经包含了。低版的tomcat要引入这个包。可以根据情况,先引入dwr.jar测试不成功,再引入xalan.jar。
   2.配置web.xml文件。
    具体的内容如下:
    <servlet>
       <servlet-name>kmweb</servlet-name>
       <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
       <load-on-startup>1</load-on-startup>

       <init-param>
         <param-name>debug</param-name>
         <param-value>true</param-value>
       </init-param>
     </servlet>
    注意:debug这个参数一定要有,是用来测试的。
     <servlet-mapping>
       <servlet-name>kmweb</servlet-name>
       <url-pattern>/ajax/*</url-pattern>
     </servlet-mapping>  
   3.编写dwr.xml文件
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE dwr PUBLIC
            "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
            "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
    <dwr>
       <allow>
         <create javascript="showName" creator="new">
         <param name="class" value="Test.A"/>
         <include method="addNode"/>
         </create>
       </allow>
    </dwr>
   这里面我已经写了一例子。当你想在页面中使用Test.A时就可以用showName来引用。include的意思就可showName对象可引用Test.A的哪些方法,不写的话就是全部的方法。
上面的是用普通的方法。如果使用的spring框架的话。配置则是另外一种法。如下:
<create creator="spring" javascript="JSUserTree">
      <param name="beanName" value="remoteUserTree"/>
      <include method="addNode"/>
      <include method="deleteNode"/>
      <include method="renameNode"/>
      <include method="getAllNodes"/>
</create>
4.编写类。
5.测试(http://服务名字或IP:端口号/项目名/ajax/)如果出来index页面就OK了
6.在测试index页面中会出现Classes known to DWR:标题.下面是我测试是的一个例子:
showName (Test.A)
hitNum (Test.ReturnInt)
这就是类与js的对应关系. 点击要要使用的类进入下一个页面.如:
Methods For: hitNum (Test.ReturnInt)
To use this class in your javascript you will need the following script includes:
<script type='text/javascript' src='/exercise/ajax/interface/hitNum.js'></script>
<script type='text/javascript' src='/exercise/ajax/engine.js'></script>
In addition there is an optional utility script:
<script type='text/javascript' src='/exercise/ajax/util.js'></script>
Replies from DWR are shown with a yellow background if they are simple or in an alert box otherwise.
The inputs are evaluated as Javascript so strings must be quoted before execution.
There are 11 declared methods:
getHitNum( ); excute
setHitNum( ); excute
hashCode() is not available: Methods defined in java.lang.Object are not accessible
getClass() is not available: Methods defined in java.lang.Object are not accessible
wait() is not available: Methods defined in java.lang.Object are not accessible
wait() is not available: Methods defined in java.lang.Object are not accessible
wait() is not available: Methods defined in java.lang.Object are not accessible
equals() is not available: Methods defined in java.lang.Object are not accessible
notify() is not available: Methods defined in java.lang.Object are not accessible
notifyAll() is not available: Methods defined in java.lang.Object are not accessible
toString() is not available: Methods defined in java.lang.Object are not accessible
点击可以进行测试了.测试成功的话.下一步就是编写您的页面.
7.在编写您的页面时一定要把
<script type='text/javascript' src='/exercise/ajax/interface/hitNum.js'></script>
<script type='text/javascript' src='/exercise/ajax/engine.js'></script>
类拟于上面的两句话加入您的页面.
8.在使用js时注意一定要一个回调函数做为最后一个参数(前提这个方法有返回值时).
9.编写回调函数,回调函数的参数就是js对应相应类的方法返回值.
10.值得注意的是如果返回的不是原始类型则必须在dwr.xml中写上如下:
<convert match="Test.Person" converter="bean"></convert>
match是返回的类型.converter表示是一个对象(确且我也不明白,明白后在写一般情况就是这).
原文地址:https://www.cnblogs.com/soundcode/p/1911885.html