dwr入門代碼二
系統
1763 0
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的操作" 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>????
記得在web.xml中配置
dwr入門代碼二
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元