[关闭]
@chy282 2018-01-09T02:51:35.000000Z 字数 6136 阅读 1374

DWR入门

dwr


© 版权声明:本文为博主原创文章,转载请注明出处

1. 作用

2. 实现

2.1 pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>org.dwr</groupId>
  5. <artifactId>DwrDemo</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <dependencies>
  9. <!-- junit -->
  10. <dependency>
  11. <groupId>junit</groupId>
  12. <artifactId>junit</artifactId>
  13. <version>4.12</version>
  14. <scope>test</scope>
  15. </dependency>
  16. <!-- dwr -->
  17. <dependency>
  18. <groupId>org.directwebremoting</groupId>
  19. <artifactId>dwr</artifactId>
  20. <version>3.0.2-RELEASE</version>
  21. </dependency>
  22. <!-- commons-logging -->
  23. <dependency>
  24. <groupId>commons-logging</groupId>
  25. <artifactId>commons-logging</artifactId>
  26. <version>1.2</version>
  27. </dependency>
  28. </dependencies>
  29. <build>
  30. <finalName>DwrDemo</finalName>
  31. <plugins>
  32. <plugin>
  33. <groupId>org.apache.maven.plugins</groupId>
  34. <artifactId>maven-compiler-plugin</artifactId>
  35. <version>3.7.0</version>
  36. <configuration>
  37. <target>1.8</target>
  38. <source>1.8</source>
  39. <encoding>UTF-8</encoding>
  40. </configuration>
  41. </plugin>
  42. </plugins>
  43. </build>
  44. </project>

2.2 web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  6. version="3.0" metadata-complete="true">
  7. <servlet>
  8. <servlet-name>dwr</servlet-name>
  9. <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
  10. <init-param>
  11. <param-name>activeReverseAjaxEnabled</param-name>
  12. <param-value>true</param-value>
  13. <description>使用服务器推技术(反转AJAX)</description>
  14. </init-param>
  15. <init-param>
  16. <param-name>crossDomainSessionSecurity</param-name>
  17. <param-value>false</param-value>
  18. <description>使能够从其他域进行请求 true:开启 false:关闭</description>
  19. </init-param>
  20. <init-param>
  21. <param-name>allowScriptTagRemoting</param-name>
  22. <param-value>true</param-value>
  23. <description>允许远程JS</description>
  24. </init-param>
  25. <init-param>
  26. <param-name>debug</param-name>
  27. <param-value>true</param-value>
  28. <description>启动调试模式</description>
  29. </init-param>
  30. </servlet>
  31. <servlet-mapping>
  32. <servlet-name>dwr</servlet-name>
  33. <url-pattern>/js/dwr/*</url-pattern>
  34. </servlet-mapping>
  35. </web-app>

2.3 java代码实现

java代码用两种方式实现

2.3.1 DwrPush.java

  1. package org.dwr.util;
  2. import java.util.Collection;
  3. import org.directwebremoting.ScriptBuffer;
  4. import org.directwebremoting.ScriptSession;
  5. import org.directwebremoting.WebContext;
  6. import org.directwebremoting.WebContextFactory;
  7. import org.directwebremoting.proxy.dwr.Util;
  8. public class DwrPush {
  9. @SuppressWarnings("deprecation")
  10. public void send(String msg) {
  11. // 获取WebContext对象,获取request,session等的辅助类
  12. WebContext webContext = WebContextFactory.get();
  13. // 获取访问页面的所有用户
  14. Collection<ScriptSession> sessions = webContext.getAllScriptSessions();
  15. // 创建ScriptBuffer对象
  16. ScriptBuffer sb = new ScriptBuffer();
  17. // 调用old_callback方法,传入参数
  18. sb.appendCall("old_callback", msg);
  19. // 根据ScriptSession创建DWR的Util对象
  20. Util util = new Util(sessions);
  21. // 回调js方法
  22. util.addScript(sb);
  23. }
  24. }

2.3.2 NewDwrPush.java

  1. package org.dwr.util;
  2. import java.util.Collection;
  3. import org.directwebremoting.Browser;
  4. import org.directwebremoting.ScriptBuffer;
  5. import org.directwebremoting.ScriptSession;
  6. public class NewDwrPush {
  7. public void send(String msg) {
  8. // 创建Runnable对象,设置回调对象以及回调方法
  9. Runnable runnable = new Runnable() {
  10. // 创建ScriptBuffer对象
  11. private ScriptBuffer script = new ScriptBuffer();
  12. @Override
  13. public void run() {
  14. // 设置要调用的js及参数
  15. script.appendCall("new_callback", msg);
  16. // 得到所有的ScriptSession对象
  17. Collection<ScriptSession> sessions = Browser.getTargetSessions();
  18. // 遍历每一个ScriptSession
  19. for (ScriptSession session: sessions) {
  20. session.addScript(script); // 将推送内容添加到ScriptSession中
  21. }
  22. }
  23. };
  24. // 执行推送
  25. Browser.withAllSessions(runnable);
  26. }
  27. }

2.4 dwr.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE dwr PUBLIC
  3. "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"
  4. "http://directwebremoting.org/schema/dwr30.dtd">
  5. <dwr>
  6. <allow>
  7. <create creator="new" javascript="DwrPush">
  8. <param name="class" value="org.dwr.util.DwrPush"/>
  9. </create>
  10. <create creator="new" javascript="NewDwrPush">
  11. <param name="class" value="org.dwr.util.NewDwrPush"/>
  12. </create>
  13. </allow>
  14. </dwr>

2.5 index.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Dwr 测试</title>
  8. </head>
  9. <body>
  10. <ul id="new_ul" style="color: red; font-size: 30px;">
  11. <li>新推送方式</li>
  12. </ul>
  13. <input type="text" name="new_msg" id="new_msg" size="30" style="height: 40px; font-size: 30px;"/>
  14. <input type="button" id="new_sign" value="新方式发布信息"/>
  15. <ul id="old_ul" style="color: red; font-size: 30px;">
  16. <li>旧推送方式</li>
  17. </ul>
  18. <input type="text" name="old_msg" id="old_msg" size="30" style="height: 40px; font-size: 30px;"/>
  19. <input type="button" id="old_sign" value="旧方式发布信息"/>
  20. <!-- 处理所有服务器通信 -->
  21. <script type="text/javascript" src="js/dwr/engine.js"></script>
  22. <!-- 帮助你用你从服务器得到的数据改变网页 -->
  23. <script type="text/javascript" src="js/dwr/util.js"></script>
  24. <script type="text/javascript" src="js/dwr/interface/DwrPush.js"></script>
  25. <script type="text/javascript" src="js/dwr/interface/NewDwrPush.js"></script>
  26. <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  27. <script type="text/javascript">
  28. $(function() {
  29. // 启动该页面的Reverse Ajax功能
  30. dwr.engine.setActiveReverseAjax(true);
  31. // 调用旧推送方式
  32. $("#old_sign").click(function() {
  33. DwrPush.send($("#old_msg").val());
  34. });
  35. // 调用新推送方式
  36. $("#new_sign").click(function() {
  37. NewDwrPush.send($("#new_msg").val());
  38. });
  39. });
  40. // 旧方式推送回调函数
  41. function old_callback(msg) {
  42. $("#old_ul").html($("#old_ul").html() + "<br/>" + msg);
  43. }
  44. // 新方式推送回调函数
  45. function new_callback(msg) {
  46. $("#new_ul").html($("#new_ul").html() + "<br/>" + msg);
  47. }
  48. </script>
  49. </body>
  50. </html>

参考:Dwr实现JAVA服务器端向客户端推送消息

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注