DWR浅尝

Posted on 2008-03-05 17:40 FaithChen 阅读(317) 评论(0)  编辑  收藏

简介:
DWR可以帮助开发人员完成应用AJAX技术的web程序,它可以让浏览器上的javascript方法调用运行在web服务器上java方法。
DWR主要由两部门组成。javascript与web服务器通信并更新web页;运行在web服务器的Servlet处理请求并把响应发回浏览器。
DWR采用新颖的方法实现了AJAX(本来也没有确切的定义),在java代码基础上动态的生成javascript代码。web开发者可以直接调用这些javascript代码,然而真正的代码是运行在web服务器上的java code。出与安全考虑,开发者必须配置哪些java class暴露给DWR.(dwr.xml)
DWR不认为浏览器和web服务器之间协议重要,把系统界面放在首位。最大挑战是java method call的同步特征与ajax异步特性之间的矛盾。在异步模型里,结果只有在方法结束后才有效。DWR解决了这个问题,把回调函数当成参数传给方法,处理完成后,自动调用回调方法。
配置:

  1. web.xml: <servlet>
        <servlet-name>dwr-invoker</servlet-name>
        <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
        <init-param>
                   <param-name>debug</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>  
                   <param-name>crossDomainSessionSecurity</param-name>   
                   <param-value>false</param-value>  
            </init-param>
       </servlet>

       <servlet-mapping>
         <servlet-name>dwr-invoker</servlet-name>
         <url-pattern>/dwr/*</url-pattern>
       </servlet-mapping>

  2. dwr.xml:<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd">
    <dwr>
     <allow>
      <create creator="spring" javascript="main_MenuCatalogDWR" scope="page">
       <param name="beanName" value="/main/MenuCatalogDWR"/>
      </create>
      <create creator="spring" javascript="main_TreeDWR" scope="page">
       <param name="beanName" value="/main/TreeDWR"/>
      </create>
      <create creator="spring" javascript="main_OperateFilterDWR" scope="page">
       <param name="beanName" value="/main/OperateFilterDWR"/>
      </create>
      <create creator="spring" javascript="main_RightTreeDWR" scope="page">
       <param name="beanName" value="/main/RightTreeDWR"/>
      </create>
      <convert converter="dom4j" match="org.directwebremoting.convert.DOM4JConverter">
      </convert>
      <convert converter="dom" match="org.directwebremoting.convert.DOMConverter">
      </convert>
     </allow>
    </dwr>

   3.jsp页面设置:
        //以下两行是dwr所必需的
        <script type='text/javascript' src="<c:url value='/dwr/engine.js'/>"></script>
                              <script type='text/javascript' src="<c:url value='/dwr/util.js'/>"></script> 
        <script type='text/javascript' src="<c:url value='/dwr/interface/main_MenuCatalogDWR.js'/>"></script>
说明:<script type='text/javascript' src="<c:url value='/dwr/engine.js'/>"></script>
            <script type='text/javascript' src="<c:url value='/dwr/util.js'/>"></script> 是dwr所必需的
   <script type='text/javascript' src="<c:url value='/dwr/interface/main_MenuCatalogDWR.js'/>"></script>主要作用是:
            dwr/interface/这种请求到来,(如上面的<script type='text/javascript' src="<c:url  value='/dwr/interface/main_MenuCatalogDWR.js'/>"></script>)DWR做一件伟大的事。把我们在WEB-INF/dwr.xml中的
           <create creator="spring" javascript="main_MenuCatalogDWR" scope="page">
                  <param name="beanName" value="/main/MenuCatalogDWR"/>
           </create>转化为javascript函数。 

4.beanName配置(本项目spring配置文件为action-servlet.xml)
 <bean name="/main/MenuCatalogDWR" class="com.icsshs.webapp.main.MenuCatalogDWR" singleton="false">
        <property name="hsRoleRightManager">
            <ref bean="hsRoleRightManager"/>
        </property>
    </bean>
5.jsp页面调用:
function menucatalog(){
     main_MenuCatalogDWR.execute("",callbackok);
    }
    function callbackok(data){
        var root0=data.documentElement;
        if (root0.childNodes.length>0){
            if (root0.childNodes(0).childNodes.length==0) return;
            $("mainmenu").innerHTML=root0.childNodes(0).childNodes(0).text;
            key=root0.childNodes(1).childNodes(0).text;
            if (parent.frames.leftFrame.document.readyState == "complete"){
                setmenuclick(key);
            }else{
                setTimeout("setmenuclick('"+key+"')",500);
            }
        }
    }

说明:callbackok为回调函数,data为dwr返回的结果集


只有注册用户登录后才能发表评论。


网站导航:
 

posts - 4, comments - 0, trackbacks - 0, articles - 4

Copyright © FaithChen