一个关于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" onclick="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;
 }
 
}

以上例子仅供参考,谢谢

你可能感兴趣的:(JavaScript,xml,servlet,css,DWR)