DWR使用 三步走

作者在 2010-02-11 10:04:46 发布以下内容

一,准备工作:

     a>.dwr.jar version:2.0.5

     b>.将获得的上述文件解压,获得以下两个依赖js:

①/WebRoot/dwr/engine.js 

②/WebRoot/dwr/util.js

二,创建web工程:

    a>.新建web工程,将jar包和js文件至于工程内。

    b>.配置web.xml配置文件:

<!-- dwr 映射配置 {begin-->
  <servlet>
   <servlet-name>dwr-invoker</servlet-name>
   <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
   <init-param>
    <param-name>debug</param-name>
    <param-value>true</param-value>
   </init-param>
  </servlet>
  <servlet-mapping>
   <servlet-name>dwr-invoker</servlet-name>
   <url-pattern>/dwr/*</url-pattern>
  </servlet-mapping>
  <!-- dwr 映射配置 end}-->

c>.添加与后台交互的java文件:

package com.sinovatech.dwr;

public class DwrService {
 
 public static String getArrayNames(String name){
  StringBuffer names = new StringBuffer();
  for (int i = 0; i < 5; i++) {
   names.append(name).append(i).append("|");
   if(i == 4){
    names.append(name).append(i);
   }
  }
  return names.toString();
 }
}

d>.添加dwr.xml配置文件与web.xml通目录:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
 <allow>
   <create creator="new" javascript="DwrService">
          <param name="class" value="com.sinovatech.dwr.DwrService"/>
       </create>
 </allow>
</dwr>

e>.测试页面index.jsp:

<%@ page language="java" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>DWR 测试工程</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <script type='text/javascript' src='<%=basePath%>/dwr/interface/DwrService.js'></script>
 <script type="text/javascript" src="<%=basePath%>/dwr/engine.js"></script>
 <script type="text/javascript" src="<%=basePath%>/dwr/util.js"></script>

 <script type="text/javascript">
  function onloadWrite(){
   //if(同步or异步执行)
       // DWREngine.setAsync(true);
       //else
       //  DWREngine.setAsync(false);
   DwrService.getArrayNames('大活宝',nameWrite);
   //回调函数
   function nameWrite(result){
    var names = result.split('|');
    for(var i=0; i<names.length; i++) {
     if(i == 0){
      document.getElementById('name').options[i] = new Option(names[i],i,true,true);
     }
     else{
      document.getElementById('name').options[i] = new Option(names[i],i,false,false);
     }
    }
   }
  }
 </script>
  </head>
 
  <body onload="onloadWrite()">
    姓名:<select name="name" id="name"></select>
  </body>
</html>

java | 阅读 985 次
文章评论,共0条
游客请输入验证码
浏览581650次