dwr入门代码二

dwr.Xml代码
<?xml version="1.0" encoding="UTF-8"?>      
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr//dwr20.dtd">      
<dwr>      
<!-- without allow, DWR isn't allowed to do anything -->      
<allow>        
         <create creator="new" javascript="Student" scope="application">          
        <param name="class" value="com.xzj.service.StudentService"/>          
    </create>      
    <convert converter="com.xzj.domain.Student" match="bean"/>      
</allow>      
</dwr>    

StudentService方法的代码如下:
Java代码
package com.xzj.service;      
    
import java.util.ArrayList;      
import java.util.List;      
    
import com.xzj.domain.Student;      
    
public class StudentService {      
          
    public  List find(){      
        List list=new ArrayList();      
        for(int k=1;k<10;k++){      
            list.add(k);      
        }      
        return list;      
    }      
          
    public Student findStudent(){      
        Student stu=new Student();      
        stu.setId(127);      
        stu.setName("小虎队");      
        stu.setAge(48);      
        return stu;      
    }      
          
    public List listStudent(){      
        List list=new ArrayList();      
        Student stu=null;      
        for(int k=1;k<6;k++){      
            stu=new Student();      
            stu.setId(k);      
            stu.setName("小猪"+k);      
            stu.setAge(23+k);      
            list.add(stu);      
        }      
        return list;      
    }      
}    

Student 的代码如下:
Java代码
package com.xzj.domain;    
  
public class Student {    
private int id;    
private String name;    
private int age;    
  
public int getId() {    
return id;    
}    
public void setId(int id) {    
this.id = id;    
}    
public String getName() {    
return name;    
}    
public void setName(String name) {    
this.name = name;    
}    
public int getAge() {    
return age;    
}    
public void setAge(int age) {    
this.age = age;    
}    
}  

前台index.jsp的代码如下:
Java代码
<%@ page language="java"  pageEncoding="UTF-8"%>      
<html>      
  <head>      
    <title>DWR Operator List and Object</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">      
    <style type="text/css">      
    a:link, a:visited{      
        margin:10px;      
        color:#A62020;      
        padding:4px 10px 4px 10px;      
        background-color: #ecd8db;      
        text-decoration:none;      
        border-top: 1px solid #EEEEEE;            
        border-left: 1px solid #EEEEEE;      
        border-bottom: 1px solid #717171;      
        border-right: 1px solid #717171;      
    }      
          
    a:hover{      
        margin:10px;      
        color:#821818;      
        padding:5px 8px 3px 12px;      
        background-color:#e2c4c9;      
        border-top:1px solid #717171;      
        border-left:1px solid #717171;      
        border-bottom:1px solid #EEEEEE;      
        border-right:1px solid #EEEEEE;      
    }      
              
    .datalist{      
        border:1px solid #5F6F7E;      
        border-collapse:collapse;      
        width:60%;      
    }      
    .datalist th{      
        border:1px solid #5F6F7E;      
        background-color:#E2E2E2;      
        color:#000000px;      
        font-weight:normal;      
        text-align:center;      
        padding:2px 8px 2px 6px;      
        height:20px;      
    }      
    .datalist td{      
        margin:0px;      
        padding:1px;      
        border:1px solid #ABABAB;      
        }      
       .put{      
        margin:0px;                  
        border:0;      
        background-color:#E2E2E2;      
        padding:5px;      
        border-bottom:1px solid #ABABAB;      
        width:auto;      
    }      
    </style>      
    <script type="text/javascript" src='dwr/interface/Student.js'></script>      
   <script type="text/javascript" src='dwr/engine.js'></script>      
   <script type="text/javascript" src='dwr/util.js'></script>      
    <script type="text/javascript">      
        function find(){      
            Student.find(showMessage);      
            function showMessage(msg){      
                var rs=new Array();      
                rs=msg      
                for(var k in rs){      
                    alert("List中的:"+rs[k]);      
                }      
            }      
        }      
              
        function findStudent(){      
            Student.findStudent(showMessage);      
            function showMessage(msg){      
                //操作Bean文件Student 必须要先再dwr.xml中配置      
                /**<convert converter="bean" match="com.xzj.domain.Student"/>*/    
                var msgStr="编号:"+msg.id+"\n姓名:"+msg.name+"\n年龄:"+msg.age;      
                alert(msgStr);      
            }      
        }      
              
        function listStudent(){      
            Student.listStudent(showMessage);      
            function showMessage(msg){      
                var rs=new Array();      
                rs=msg;      
                var table="<table  class='datalist'>";      
                        table+="<tr>";      
                            table+="<th>编号</th>";      
                            table+="<th>姓名</th>";      
                            table+="<th>年龄</th>";      
                        table+="</tr>";      
                for(var k in rs){      
                    table+="<tr>";      
                        table+="<th>"+rs[k].id+"</th>";      
                        table+="<td>"+rs[k].name+"</td>";      
                        table+="<td>"+rs[k].age+"</td>";      
                    table+="</tr>";      
                }      
                table+="</table>";      
                showMsg.innerHTML=table;      
            }      
        }      
    </script>      
  </head>      
        
  <body>      
    <center>      
        <input type="button" class="put" name="btnList" value="查看对List的操作" οnclick="find()"/>      
        <input type="button" class="put" name="btnList" value="查看对Student对象的操作" οnclick="findStudent()"/>      
        <input type="button" class="put" name="btnList" value="查看对List中5个Student对象的操作" οnclick="listStudent()"/>      
    </center>      
    <br><br>      
    <br><br>      
    <div id="showMsg" style="border:1px dashed #CCCCCC;width:500px:height:auto;margin:5px;padding:5px;text-align:center;">      
          
    </div>      
  </body>      
</html>    
记得在web.xml中配置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值