dwr实例

  dwr是一种很好的ajax框架,与ajax的另外一种框架ext相辅相成,既dwr注重与服务器端通信,而且ext注重页面的展现。下面来看一个简单的dwr配置.

 

  第一:web.xml配置如下

<servlet>
    <servlet-name>dwr-invoker</servlet-name>
    <display-name>DWR Servlet</display-name>
    <description>Direct Web Remoter Servlet</description>
    <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
    <!--
    <init-param>
      <param-name>config</param-name>
      <param-value>WEB-INF/dwr.xml</param-value>
    </init-param>
    -->
    <init-param>
      <param-name>debug</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>scriptCompressed</param-name>
      <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

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

 

 

第二:在WEB-INF里面加文件dwr.xml,其中UserManager为java类

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" 
"http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
  <init>
  </init>
  <allow>
    <create creator="new" javascript="JUserManager" scope="application">
    	<param name="class" value="UserManager"/>
    </create>
 </allow>
</dwr>

  

  第三:在lib中加dwr.jar

 

  第四:客户端调用

<script type='text/javascript' src='/water/dwr/interface/JUserManager.js'></script>
<script type='text/javascript' src='/water/dwr/engine.js'></script>
//***********************************************部分删除操作*****************************************************//
 function remove(){
                var record = grid.getSelectionModel().getSelected();// 返回值为 Record 类型
                if(!record){
     Ext.Msg.alert("提示","请先选择要删除的行!");
     return;
    }
    // 弹出对话框警告
    if(record) {
     Ext.MessageBox.confirm('确认删除', 
            '你真的要删除所选用户吗?', 
            function(btn) {
             if(btn == 'yes') {// 选中了是按钮
               // 调用 DWR, 执行结果成功时方删除所有数据
              JUserManager.removeUser("<%=obj_name%>",record.get("id"), function(data) {
               if(data) {
                // 更新界面, 来真正删除数据
          ds.remove(record);
         } else {
          Ext.Msg.alert("出错了!","单个用户数据删除失败!");
         }
        });
        
       }
      }
     );
    }
            }

  

你可能感兴趣的:(JavaScript,Ajax,servlet,ext,DWR)