dwr入门代码二
系统
1676 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元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。
【本文对您有帮助就好】元