锁定老帖子 主题:DWR操作 (二) 操作对象和集合
精华帖 (0) :: 良好帖 (0) :: 新手帖 (7) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-08-27
之前的基本配置一样。这里我就只把 dwr.xml 和Service 和bean 还有jsp页面的代码展示出来 <?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的好处了吧。不过..对于他的不好之处。你可以去网络中看看. 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-08-28
里面有些错误,<convert converter="bean" match="com.xzj.domain.Student"/>
反了 ,如果不想告诉别人那为什么还要写出来,如果写出来还是错的还不如不写,dwr操作1也有错 都不知道写出来干嘛 |
|
返回顶楼 | |
发表时间:2008-08-28
楼上的,你要是厉害的话你也发表啊,我看你也不见得怎么样...
楼主的代码没有问题,我试过了,如果按代码不成功的话,可能是没有配置web.xml. |
|
返回顶楼 | |
发表时间:2008-08-29
我上面的都是正确的啊。。
我都是做完了。测试完了。再发布的。。 |
|
返回顶楼 | |
发表时间:2008-09-11
写出来总比没有的强,不像有些人,自己不写还不断的指出别人的错误,指出错误当然是好的,可也不能像人生攻击一样啊。
自以为高手, |
|
返回顶楼 | |
发表时间:2008-09-12
支持楼主 希望多一些这样的文章
|
|
返回顶楼 | |
发表时间:2008-09-18
l楼的难道说是我的那个顺序上下 写反了???
如果是这样的。。那你就是个 只会版版套的人... |
|
返回顶楼 | |
发表时间:2008-10-13
请问楼主,你如何处理像Hibernate返回的List 对象?我的意思是说这个List中包含N个对象,而每个对象里面又包含N个集合,也就是一对多关系的对象,
|
|
返回顶楼 | |
发表时间:2008-10-23
ssy8110 写道 请问楼主,你如何处理像Hibernate返回的List 对象?我的意思是说这个List中包含N个对象,而每个对象里面又包含N个集合,也就是一对多关系的对象, 这个,暂时还没做过。。 有时间做了传上来。。谢谢提醒。 |
|
返回顶楼 | |
发表时间:2008-12-10
写的太好了..希望能够看到更多的例子出现
|
|
返回顶楼 | |