代码改变世界

dwr配置

2012-11-26 09:52  hduhans  阅读(275)  评论(0)    收藏  举报

一、下载相关jar包

     ①dwr3.0

     ②commons-logging.jar

 

二、配置dwr.xml(放在WEB-INF下)

 <?xml version="1.0" encoding="UTF-8"?>
     <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.or  g/dwr//dwr30.dtd">
     <dwr>
     <allow>
           <create creator="new" javascript="Panchina_ChatSysJs">
                 <param name="class" value="com.panchina.chatsys.Panchina_ChatSys" />
           </create>
     </allow>
    </dwr>

 

 

三、修改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>true</param-value>
      </init-param>
      <init-param>
         <param-name>pollAndCometEnabled</param-name>
         <param-value>true</param-value>
      </init-param>
      <init-param>
         <param-name>activeReverseAjaxEnabled</param-name>
         <param-value>true</param-value>
      </init-param>
      <init-param>  
          <param-name>maxWaitAfterWrite</param-name>  
          <param-value>50</param-value>  
      </init-param>  
      <load-on-startup>4</load-on-startup>
   </servlet>
   <servlet-mapping> 
      <servlet-name>dwr-invoker</servlet-name>
      <url-pattern>/dwr/*</url-pattern>
   </servlet-mapping>

 

 

四、在需要调用的页面中添加对应脚本

   

<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script type='text/javascript' src='dwr/interface/Panchina_ChatSysJs.js'></script>

 

 

注:①在需要调用dwr脚本的页面中添加脚本 dwr.engine.setActiveReverseAjax(true);  否则服务器无法立即将数据推送到服务器

     ②dwr回调函数有多个参数时 调用方法如下:PublicChatJS.getServerTime(function(serverParam){test(serverParam,localParam)});

 

转发参考代码:

参考一:

WebContext wctx = WebContextFactory.get();
String currentPage = wctx.getCurrentPage();
ScriptBuffer script = new ScriptBuffer();
Collection pages = wctx.getScriptSessionsByPage(currentPage);
script.appendScript("setUserOnlineById('"+userId+"');");
for (Iterator it = pages.iterator(); it.hasNext();) {
  ScriptSession otherSession = (ScriptSession) it.next();
  otherSession.addScript(script);
}

 

参考二:

WebContext wctx = WebContextFactory.get(); // 这里是获取WebContext上下文
String clientPage = "/Pop/index.jsp";// 推到哪个页面

Collection sessions = wctx.getScriptSessionsByPage(clientPage); // 在一个page中可能存在多个ScriptSessions
Util utilAll = new Util(sessions); // Util 是DWR 在Server端模拟Brower端
utilAll.addFunctionCall("callFunc", "");         //客户端js函数