`
xzj127
  • 浏览: 15683 次
  • 性别: Icon_minigender_1
  • 来自: 帝都火星村
社区版块
存档分类
最新评论

DWR操作 (二) 操作对象和集合

阅读更多

之前的基本配置一样。这里我就只把 dwr.xml 和Service 和bean 还有jsp页面的代码展示出来
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="bean" match="com.xzj.domain.Student"/>   
</allow>   
</dwr>  

 

StudentService方法的代码如下:

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("DWR冰山"+k);   
            stu.setAge(23+k);   
            list.add(stu);   
        }   
        return list;   
    }   
}  

  

 

Student 的代码如下:

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的代码如下:

<%@ 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的操作" onclick="find()"/>   
        <input type="button" class="put" name="btnList" value="查看对Student对象的操作" onclick="findStudent()"/>   
        <input type="button" class="put" name="btnList" value="查看对List中5个Student对象的操作" onclick="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>  

 

 

 

为了好看 弄了点 CSS 代码。弄完这个。可以自由的操作DWR了。应该知道DWR的好处了吧。不过..对于他的不好之处。你可以去网络中看看.
如果明天有时间 继续
关于Spring的操作.
这里 我没有对数据库进行操作 。因为都是一样的...

  • dwr02.rar (455.3 KB)
  • 描述: 对应例子
  • 下载次数: 305
分享到:
评论
12 楼 yangpanwww 2009-07-01  
Lz   这个问题我知道了。。我还有个问题

当 dwr 返回 一个 对象时 比如: user.java 里有 name,age 的get,set方法

我想把这个对象 使用 Struts 标签 <bean:write />输出。。。

怎么实现。。。。???
11 楼 yangpanwww 2009-06-30  
LZ   我下载了你的工程。。但是。。运行时报了 js 错误;

"Student" 未定义。。。。


Student.js 不是自动生成的吗?
10 楼 juzhibest 2009-05-15  
果然很受用.谢谢LZ
9 楼 vampires 2008-12-10  
写的太好了..希望能够看到更多的例子出现
8 楼 xzj127 2008-10-23  
ssy8110 写道

请问楼主,你如何处理像Hibernate返回的List 对象?我的意思是说这个List中包含N个对象,而每个对象里面又包含N个集合,也就是一对多关系的对象,

这个,暂时还没做过。。
        有时间做了传上来。。谢谢提醒。
7 楼 ssy8110 2008-10-13  
请问楼主,你如何处理像Hibernate返回的List 对象?我的意思是说这个List中包含N个对象,而每个对象里面又包含N个集合,也就是一对多关系的对象,
6 楼 xzj127 2008-09-18  
l楼的难道说是我的那个顺序上下 写反了???

      如果是这样的。。那你就是个 只会版版套的人...
5 楼 llm6101 2008-09-12  
支持楼主 希望多一些这样的文章
4 楼 phenom 2008-09-11  
写出来总比没有的强,不像有些人,自己不写还不断的指出别人的错误,指出错误当然是好的,可也不能像人生攻击一样啊。
自以为高手,
3 楼 xzj127 2008-08-29  
我上面的都是正确的啊。。
         我都是做完了。测试完了。再发布的。。
2 楼 xiaoqiang01 2008-08-28  
楼上的,你要是厉害的话你也发表啊,我看你也不见得怎么样...


楼主的代码没有问题,我试过了,如果按代码不成功的话,可能是没有配置web.xml.
1 楼 pavel 2008-08-28  
里面有些错误,<convert converter="bean" match="com.xzj.domain.Student"/>     
反了 ,如果不想告诉别人那为什么还要写出来,如果写出来还是错的还不如不写,dwr操作1也有错
都不知道写出来干嘛

相关推荐

Global site tag (gtag.js) - Google Analytics