只贴代码,其它的就不说了
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<init>
</init>
<allow>
<create creator="new" javascript="qbusses">
<param name="class" value="ajax.Qbusses"/>
</create>
<create creator="new" javascript="pagination">
<param name="class" value="domain.Pagination"/>
</create>
<convert converter="bean" match="domain.Person">
<param name="include" value="username,email,age"/>
</convert>
</allow>
<signatures>
<![CDATA[
import java.util.*;
import ajax.Qbusses;
import domain.Person;
Qbusses.addMorePerson(List<Person>);
]]>
</signatures>
</dwr>
业务类
package ajax;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.directwebremoting.*;
import org.apache.commons.lang.*;
import java.util.*;
import domain.Person;
public class Qbusses {
public static List <Person> slist=new ArrayList();
static{
slist.add(new Person("小裴","小裴@gmail.com",1));
slist.add(new Person("小李","小李@gmail.com",2));
slist.add(new Person("小东","小东@gmail.com",3));
slist.add(new Person("小西","小西@gmail.com",4));
slist.add(new Person("小坏蛋","小坏蛋@gmail.com",5));
slist.add(new Person("小鸭","小鸭@gmail.com",6));
slist.add(new Person("小鸡","小鸡@gmail.com",7));
slist.add(new Person("小狗","小狗@gmail.com",8));
slist.add(new Person("小猪","小猪@gmail.com",9));
slist.add(new Person("小洪","小洪@gmail.com",10));
slist.add(new Person("小飞","小飞@gmail.com",11));
slist.add(new Person("小郑","小郑@gmail.com",12));
slist.add(new Person("小古","小古@gmail.com",13));
slist.add(new Person("小猫","小猫@gmail.com",14));
slist.add(new Person("小红","小红@gmail.com",15));
slist.add(new Person("小黑","小黑@gmail.com",16));
slist.add(new Person("Think Pad","Think@gmail.com",17));
}
public String getSessionAndRequestData(){
String result="";
org.directwebremoting.WebContext wc=org.directwebremoting.WebContextFactory.get();
HttpSession session=wc.getSession();
String str_session=(String)session.getAttribute("sess");
HttpServletRequest request=wc.getHttpServletRequest();
String str_request=(String)request.getAttribute("req");
if(StringUtils.isEmpty(str_session)){
str_session="session等于空";
}
if(StringUtils.isEmpty(str_request)){
str_request="request等于空";
}
result=str_session+"-"+str_request;
return result;
}
/**
* 摸拟不刷新页面翻页,每页三条记录
* @param page 查询页数
* @return
*/
public List<Person > QueryPersonList(int page ){
List <Person>result=new ArrayList<Person>();
if(page==0){
result=slist;
}else{
page=page<1?1:page;
int start=3*(page-1);
int end =3*page;
result=slist.subList(start,end);
}
return result;
}
/**
* 添加person
* @param person
*/
public void addPerson(Person person){
slist.add(person);
}
/**
* 修改Person
* @param person
*/
public void modifyPerson(Person person){
Person temp=null;
for(Iterator it=slist.iterator();it.hasNext();){
temp=(Person) it.next();
if(temp.getAge()==person.getAge()){
slist.remove(temp);
slist.add(person);
return;
}
}
}
/**
* 获取序列的person对象
* @param pos
* @return
*/
public Person getPerson(int pos){
Person person=null;
person=slist.get(pos);
return person;
}
/**
* 以list做为参数的方法
* @param plist
* @return
*/
public String addMorePerson(List<Person>plist){
System.out.println(plist.size());
return "添加的记录长度是: "+plist.size();
}
/**
* 返回list的方法
* @return
*/
public List getPersonList(){
List <Person>result=new ArrayList<Person>();
result.add(slist.get(0));
result.add(slist.get(1));
return result;
}
}
接下来domain类
package domain;
import ajax.Qbusses;
public class Pagination {
private int page;
private int rowcount;//记录总行数
private int pagerowcount=5;//每页行记录数
private int totalpage; //总页数
public Pagination(int rowcount){
this.rowcount=rowcount;
totalpage=rowcount%pagerowcount;
if(rowcount%pagerowcount>0){
totalpage=totalpage+1;
}
}
public Pagination(){
this.rowcount=Qbusses.slist.size();
totalpage=rowcount%pagerowcount;
if(rowcount%pagerowcount>0){
totalpage=totalpage+1;
}
}
public int getFirst(int page){
this.page=page;
return page;
}
public int getNext(int page){
if(page==totalpage){
return totalpage;
}else{
this.page=page;
return page+1;
}
}
public int getUp(int page){
if(page==1){
return page;
}else{
this.page=page-1;
return page-1;
}
}
public int getLast(int page){
this.page=totalpage;
return this.totalpage;
}
}
package domain;
public class Person {
private String username;
private String email;
private int age;
public Person(){
}
public Person(String username,String email,int age){
this.username=username;
this.email=email;
this.age=age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
页面代码
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/qbusses.js'></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/pagination.js'></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'></script>
<title>DWR测试</title>
<style>
.usersTable {
background: #cccccc;
}
.usersTable thead{
background: #f5f5f5;
}
.usersTable tbody td{
font-size:12px;
}
.selected{
color:#FFFFFF;
font-weight:bold;
border:1px solid red;
background-color: #0099FF;
}
#datalist {
width: 80%;
border: 1px solid #CCCCCC;
}
</style>
<script>
function call_getSessionAndRequestData(){
qbusses.getSessionAndRequestData(back_call_getSessionAndRequestData);
}
function back_call_getSessionAndRequestData(data){
DWRUtil.setValue("data_1",data);
}
var page=1;
function first(){
pagination.getFirst(1,loadTableData);
}
function up(){
pagination.getUp(page,loadTableData);
}
function next(){
pagination.getNext(page,loadTableData);
}
function last(){
pagination.getLast(page,loadTableData);
}
function loadTableData(vpage) {
page=vpage;
qbusses.QueryPersonList(vpage,fillTable);
}
function fillTable(users) {
alert("返回记录长度->"+users.length);
DWRUtil.removeAllRows("usersBody");
DWRUtil.addRows("usersBody", users, cellFuncs);
addEvent();
}
var cellFuncs=[
function(data) {return data.username;},
function(data) {return data.email;},
function(data) {return data.age;}
];
var cruRow=false;
function addEvent(){
var trk=usersBody.firstChild;
while(trk){
trk.onclick=rowclick;
trk=trk.nextSibling;
}
}
function rowclick(){
var cruRow1=event.srcElement;
var obj=cruRow1.parentNode;
var temp=obj.style.backgroundColor;
//alert(obj.style.backgroundColor);
if(usersBody.selectedObj!=null)
usersBody.selectedObj.className="";
obj.className="selected";
//alert(obj.outerHTML);
//alert(obj.style.bgColor);
cruRow=obj;
//obj.style.color="#010101";
document.all("username").value= cruRow.cells[0].innerText;
document.all("email").value= cruRow.cells[1].innerText;
document.all("age").value= cruRow.cells[2].innerText;
usersBody.selectedObj=cruRow;
}
function do_add(){
var person={username:"",email:"",age:""};
DWRUtil.getValues(person);
qbusses.addPerson(person);
loadTableData();
}
function do_modify(){
var person={username:"",email:"",age:""};
DWRUtil.getValues(person);
qbusses.modifyPerson(person);
loadTableData();
}
function back_seePerson(data){
for(var property in data){
alert(property+"-"+data[property]);
}
}
function seePerson(){
var pos=document.all("pos").value;
qbusses.getPerson(pos,back_seePerson);
}
function dolistparame(){
var data;
data=[
{username:'abc',email:document.getElementById("xemail").value,age:'56'},
{username:'cced',email:'cced@qq.com',age:document.getElementById("xage").value}
];
qbusses.addMorePerson(data,function(data){alert(data);} );
}
//返回list的方法
function callReturnList(){
qbusses.getPersonList(showListResult);
}
function showListResult(data){
for(var i=0;i<data.length;i++){
alert(data[i].username+"-"+data[i].email+"-"+data[i].age);
}
}
</script>
</head>
<body onLoad="call_getSessionAndRequestData(),first()">
<%
session.setAttribute("sess","session value");
request.setAttribute("req","req value");
%>
<div style="border :1px #f0f0f0 solid" id="data_1">
</div>
<div id="datalist">
<a href="javaScript:first()">第一页</a>
<a href="javaScript:up()">上一页</a>
<a href="javaScript:next()">下一页</a>
<a href="javaScript:last()">最后一页</a>
<table width="524" cellspacing="1" bgcolor="#cccccc" class="usersTable" id="usersTable">
<thead>
<tr>
<td bgcolor="#f5f5f5">用户名</td>
<td bgcolor="#f5f5f5">Email</td>
<td bgcolor="#f5f5f5">年龄</td>
</tr>
</thead>
<tbody id="usersBody">
</tbody>
</table>
</div>
<br>
<table width="80%" border="0" cellpadding="0" cellspacing="1" bgcolor="#cccccc">
<tr>
<td width="23%" bgcolor="#f7f7f7">用户名</td>
<td width="77%" bgcolor="#ffffff">
<input type="text" name="username" >
</td>
</tr>
<tr>
<td bgcolor="#f7f7f7">Email</td>
<td bgcolor="#ffffff">
<input type="text" name="email">
</td>
</tr>
<tr>
<td bgcolor="#f7f7f7">年龄</td>
<td bgcolor="#ffffff"><input type="text" name="age"></td>
</tr>
<tr>
<td colspan="2" align="center" bgcolor="#f5f5f5">
<input type="button" name="btn_modify" value="保存" onClick="do_add()">
<input type="button" name="btn_modify" value="修改" onClick="do_modify()">
</td>
</tr>
</table>
<input type="hidden" id="xemail" value="xkd@qq.com" >
<input type="hidden" id="xage" value="888" >
<br>
<input type="text" name="pos" value="0" > <button onclick="seePerson()">查询</button><Br>
<button onclick="dolistparame()">调用list做参数的方法</button><Br>
<button onclick="callReturnList()">调用返回List的方法</button>
</body>
</html>
posted on 2007-10-19 15:10
有猫相伴的日子 阅读(1471)
评论(0) 编辑 收藏 所属分类:
dwr