/**
**QQ:252574345
**MSN:lele_love_lomboz@hotmail.com
*/
刚才顺手帮朋友写了个AJAX局部更新表格的例子,顺便贴出来(后面有整个工程,提供下载)
1、业务类
package com.zyl.dwr.impl;
public class StatusBusiness {
/**
* 测试方法
* @param str
* @return 被测试字符串
*/
public String testDWR(String str)
{
return "1";
}
}
2、页面
<%@ 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">
<title>DWR测试</title>
<script type="text/javascript" src='<%= request.getContextPath() %>/dwr/interface/bu.js'></script>
<script type='text/javascript' src='<%= request.getContextPath() %>/dwr/engine.js'></script>
<script type='text/javascript' src='<%= request.getContextPath() %>/dwr/util.js'></script>
</head>
<script>
function operate(newsID,str)
{
if(confirm("删除新闻"+'“'+str+'”'+"吗?"))
{
bu.testDWR(newsID,operatex);
}
function operatex(data)
{
var e=document.getElementById("news");
for(var i=0;i<e.rows.length;i++)
{
var row=e.rows[i];
if(row.id!=null && row.id!="" && row.id==data)
{
e.deleteRow(i);
i=i-1;
}
}
alert("删除新闻成功");
}
}
</script>
<body>
本程序,在指定表格内使用AJAX局部更新表格中数据
由于并没有连接数据库操作,因此,从前台调用后台的业务方法返回的是固定值1,所以只会删除第一条数据
<form action="" name="post">
<table id="news" cellspacing="2" cellpadding="2" align="center" border="2" bgcolor="#c0c0c0">
<tr>
<td width="15%">名称</td><td width="55%">内容</td><td>操作</td>
</tr>
<tr id="1">
<td>国内</td><td>风调雨顺</td><td><a href="#" onclick='operate("1","国内");return false;'>删除</a></td>
</tr>
<tr id="2">
<td>国外</td><td>地震不断</td><td><a href="#" onclick='operate("2","国外");return false;'>删除</a></td>
</tr>
<tr id="3">
<td>世界</td><td>风景这边最好</td><td><a href="#" onclick='operate("3","世界");return false;'>删除</a></td>
</tr>
</table>
</form>
</body>
</html>
3、dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<allow>
<create creator="new" javascript="bu">
<param name="class" value="com.zyl.dwr.impl.StatusBusiness"/>
<include method="testDWR"/>
</create>
<create creator="new" javascript="re">
<param name="class" value="forbest.tool.SelectA"/>
<include method="compareNewsID1"/>
</create>
</allow>
</dwr>
4.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>20</session-timeout>
</session-config>
</web-app>