自己做的一个关于DWR的例子。

自己做的一个关于DWR的例子。

 

index.jsp

-------------------------------------------

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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  DEMO</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">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
  <script type='text/javascript' src='/DWRDemo/dwr/interface/DWRExample.js'></script>
  <script type='text/javascript' src='/DWRDemo/dwr/engine.js'></script>
  <script type='text/javascript' src='/DWRDemo/dwr/util.js'></script>

 

  <script type="text/javascript">
 
  function show()
  {
   //display list data
    DWRExample.getList(call);
  }
  function call(data)
  {
   if(data)
   {
   var content="name    age<br/>";
    for(var i=0;i<data.length;i++)
    {
     //alert(data[i].name);
     content+=(data[i].name+"  "+data[i].age+"<br/>")
    }
    DWRUtil.setValue("DemoDiv",content);
   
   }
  }
 
 
  function validateForm()
  {
  //validate user name
   //var name=document.getElementById("name");
   var name=DWRUtil.getValue("name");
   if(name==null || name=="")
   {
    alert("name is null!");
   }else
   {
    
  DWRExample.checkUser(name,callBack);
   
   }
  }
  function callBack(data)
  {  
   if(data)
   {
    //alert("name is right!");
    //name=='xy' return to current page
    window.location.href="/DWRDemo/";
   }else
   {
    alert("name is not exist!");
   }
  }
 
  </script>
  <body>
    This is my DWR DEOM page. <hr>
    <input id="name"  name="name" value=""/>
    <input  type="button" οnclick="show();"  value=" ok "/>
   
    <br>
    <div id="DemoDiv">demo</div>
  </body>
</html>
***************************************

dwr.xml

-------------------------------------------------

 

<?xml version="1.0" encoding="UTF-8"?>
<dwr>
<!-- AJAX技术具体实现 -->
    <allow>
    <!-- convert将bean的集合变成javascript中的对象数组-->
        <convert  converter="bean" match="com.DemoBean"></convert>

       
<!-- 业务处理类 -->
        <create creator="new" javascript="DWRExample" class="com.DWRExample" scope="session">
     <include method="getList"/> 
     <include method="checkUser"/>   
        </create>       
   </allow>
</dwr>

 

********************************

web.xml

----------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <!-- DWR2.0  -->
   <servlet>
    <servlet-name>dwrInvoker</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>classes</param-name>
    <param-value>java.lang.Object</param-value>
    </init-param>
     
      <load-on-startup>10</load-on-startup>
  </servlet>

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


</web-app>

***************************************]

DWRExample .java

-------------------------------------------------

package com;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class DWRExample implements Serializable {

 public List getList()
 {
  List list=new ArrayList();
  DemoBean db=null;
  for(int i=0;i<5;i++)
  {
   db=new DemoBean();
   db.setAge(22+i);
   db.setName("ddh"+i);  
   list.add(db);
  }
   
  
  return list;
  
 }
 
 public boolean checkUser(String name)
 {
  if("xy".equals(name))
  {
   return true;
  }else
  {
   return false;
   
  }
 }
}

***********************************

DemoBean .java

---------------------------------------------

package com;

import java.io.Serializable;

public class DemoBean implements Serializable {

 
 private String name;
 private int age;
 
 
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 
}

 

以上例子仅供参考,谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值