DWR基础入门使用-第一节

前端之家收集整理的这篇文章主要介绍了DWR基础入门使用-第一节前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.功能目的:

这个系列是想通过DWR最终实现服务器到客户端的推送,形象描述类似于在页面不刷新的前提下,页面会接收到服务器推送给当前用户的消息,以气泡形式展现。

2.为何使用DWR:

这里可以参考我博客中的其他文章,很大部门摘自于网络。十分感谢这些具备分享精神的同仁们。我这里就不详细阐述了。


3.DWR学习的一些相关资料:

官网地址:

http://directwebremoting.org/dwr/

其实针对于DWR的学习,官网已经给了非常详尽的资料了。在线demo,简单的部署教程以及API等等非常详细了。

学习demo下载地址:http://download.csdn.net/download/techbirds_bao/5505799

4.helloword的dwr基础入门

目录结构:

步骤

1.jar包导入

2.web.xml配置

  1. <web-app id="dwr">
  2.  
  3. <display-name>DWR (Direct Web Remoting)</display-name>
  4. <description>A Simple Demo DWR</description>
  5.  
  6. <servlet>
  7. <servlet-name>dwr-invoker</servlet-name>
  8. <display-name>DWR Servlet</display-name>
  9. <description>Direct Web Remoter Servlet</description>
  10. <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
  11.  
  12. <!-- This should NEVER be present in live -->
  13. <init-param>
  14. <param-name>debug</param-name>
  15. <param-value>true</param-value>
  16. </init-param>
  17.  
  18. <!-- Remove this unless you want to use active reverse ajax -->
  19. <init-param>
  20. <param-name>activeReverseAjaxEnabled</param-name>
  21. <param-value>true</param-value>
  22. </init-param>
  23.  
  24. <!-- By default DWR creates application scope objects when they are first
  25. used. This creates them when the app-server is started -->
  26. <init-param>
  27. <param-name>initApplicationScopeCreatorsAtStartup</param-name>
  28. <param-value>true</param-value>
  29. </init-param>
  30.  
  31. <!-- This enables full streaming mode. It's probably better to leave this
  32. out if you are running across the internet -->
  33. <init-param>
  34. <param-name>maxWaitAfterWrite</param-name>
  35. <param-value>-1</param-value>
  36. </init-param>
  37.  
  38. <!--
  39. For more information on these parameters,see:
  40. - http://getahead.org/dwr/server/servlet
  41. - http://getahead.org/dwr/reverse-ajax/configuration
  42. -->
  43.  
  44. <load-on-startup>1</load-on-startup>
  45. </servlet>
  46.  
  47. <servlet-mapping>
  48. <servlet-name>dwr-invoker</servlet-name>
  49. <url-pattern>/dwr/*</url-pattern>
  50. </servlet-mapping>
  51.  
  52. </web-app>

3.dwr.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">
  3.  
  4. <dwr>
  5.  
  6. <allow>
  7. <!-- base -->
  8. <create creator="new" javascript="Say" scope="application">
  9. <param name="class" value="org.getahead.dwrdemo.base.Say"/>
  10. </create>
  11.  
  12.  
  13. </allow>
  14.  
  15. </dwr>

4.dwr后台调用java类编写

  1. package org.getahead.dwrdemo.base;
  2.  
  3. public class Say {
  4. public String hello(){
  5. return "hello";
  6. }
  7. public String world(){
  8. return "world";
  9. }
  10. }

5.页面调用

  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. <script type='text/javascript' src='../dwr/engine.js'> </script>
  9. <script type='text/javascript' src='../dwr/interface/Say.js'> </script>
  10. <script type='text/javascript' src='../dwr/util.js'> </script>
  11.  
  12. </head>
  13. <body onload="dwr.engine.setActiveReverseAjax(true);">
  14. <input type="button" value="say hello" onclick="sayHello();"/>
  15. <br/>
  16. <input type="button" value="say world" onclick="sayWorld();"/>
  17. </body>
  18. <script type="text/javascript">
  19. function sayHello(){
  20. Say.hello(function(data){
  21. alert(data);
  22. });
  23. }
  24. function sayWorld(){
  25. Say.world(function(data){
  26. alert(data);
  27. });
  28. }
  29. </script>
  30. </html>

猜你在找的Ajax相关文章