关于使用DWR框架的反向Ajax技术时,服务器只访问了一次我自己的业务方法,其他的都在访问dwr/call/plainpoll/ReverseAjax.dwr

ganchunsaixx 2012-08-20 11:20:26
目前想做这样一个功能:
实时显示数据库的情况,也就是数据库增、删、改、查后能将数据实时的显示到页面。
使用DWR2.X的推技术。

数据库的数据如下:
编号 姓名 年龄 家庭地址
1 张三 18 广东省广州市天河区
2 李四 18 广西省贵港市
3 齐铭 18 云南省昆明市

[code=web.xml][/code]
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- 推送 -->
<servlet>
<servlet-name>dwr-studentcomet-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>

<!-- DWR默认采用piggyback方式 -->

<!-- 使用polling和comet的方式 -->
<init-param>
<param-name>pollAndCometEnabled</param-name>
<param-value>true</param-value>
</init-param>

<!-- comet方式 -->
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>

<!-- polling方式:在comet方式的基础之上,再配置以下参数 -->
<init-param>
<param-name>org.directwebremoting.extend.ServerLoadMonitor</param-name>
<param-value>org.directwebremoting.impl.PollingServerLoadMonitor</param-value>
</init-param>

<!-- 毫秒数。页面默认的请求间隔时间是5秒 -->
<init-param>
<param-name>timeToNextPoll</param-name>
<param-value>1000</param-value>
</init-param>

<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dwr-studentcomet-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>


[code=dwr.xml][/code]
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.ltd.uk/dwr/dwr20.dtd">

<dwr>
<allow>
<create javascript="DWRMethod" creator="new" scope="application">
<param name="class" value="biz.StudentBiz"></param>
<include method="getStudents"/>
</create>
<convert converter="bean" match="bean.*" />
<convert converter="list" match="java.util.List" />
</allow>
</dwr>

[code=Student.java(javabean)][/code]
package bean;

import java.io.Serializable;

public class Student implements Serializable {

public Student() {
super();
}

public Student(int stuId, String stuName, int stuAge, String stuAddress) {
this.stuId = stuId;
this.stuName = stuName;
this.stuAge = stuAge;
this.stuAddress = stuAddress;
}

public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String getStuAddress() {
return stuAddress;
}
public void setStuAddress(String stuAddress) {
this.stuAddress = stuAddress;
}

private int stuId;
private String stuName;
private int stuAge;
private String stuAddress;
}


[code=StudentBiz.java][/code]
package biz;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;
import dao.DBConnection;
import bean.Student;

public class StudentBiz {
private static ReentrantLock lock = new ReentrantLock();

public void getStudents(){
Connection con = DBConnection.getConnection();
Statement sm = null;
ResultSet rs = null;
List<Student> studentliList = new ArrayList<Student>();

System.out.println("----------");
try {
//获取锁定
lock.lock();
String sql = "SELECT * FROM STUDENT";
sm = con.createStatement();
rs = sm.executeQuery(sql);
while(rs.next()){
Student s = new Student(rs.getInt("stu_id"),rs.getString("stu_name"),rs.getInt("stu_age"),rs.getString("stu_address"));
studentliList.add(s);
}
DBConnection.close(con,sm,rs);
} catch (Exception e) {
e.printStackTrace();
}finally{
//释放锁定
lock.unlock();
}
System.out.println("1、获得DWR上下文");
WebContext webContext = WebContextFactory.get();

System.out.println("2、获取当前页面的URL");
String currentPage = webContext.getCurrentPage();

System.out.println("3、当前脚本session");
ScriptSession scriptSession = webContext.getScriptSession();

// System.out.println("4、设置页面控件的值");
// Util util = new Util(scriptSession);
// util.setValue("text", ""); //清空页面控件的值

System.out.println("5、设置脚本session的属性值");
scriptSession.setAttribute("list", studentliList);

System.out.println("6、获取脚本session的属性值");
for(Iterator it = scriptSession.getAttributeNames(); it.hasNext();){
String attributeName = (String)it.next();
System.out.println(attributeName + "=" + scriptSession.getAttribute(attributeName));
}

System.out.println("7、获取所有浏览但前页面的脚本session");
Collection<ScriptSession> sessions = webContext.getScriptSessionsByPage(currentPage);

System.out.println("8、--------");
Util allUtil = new Util(sessions);

System.out.println("9、执行客户端脚本");
ScriptBuffer script = new ScriptBuffer();
script.appendScript("clientFunction(")
.appendData(scriptSession.getAttribute("list"))
.appendScript(");");

System.err.println("10、-------");
for(ScriptSession session : sessions){
session.addScript(script);
}

/*System.out.println("11、更新这些脚本session的一些元素");
for(int i = 0; i < studentliList.size(); i ++){
Student su = studentliList.get(i);
allUtil.setValue("id", String.valueOf(su.getStuId()));
allUtil.setValue("name", su.getStuName());
allUtil.setValue("age", String.valueOf(su.getStuAge()));
allUtil.setValue("address", su.getStuAddress());
}
/*allUtil.removeAllOptions("messages");
allUtil.addOptions("messages", studentliList, "id", "text");*/
}
}

[code=jsp][/code]
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>学生信息</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="<%=path %>/dwr/util.js"></script>
<script type="text/javascript" src="<%=path %>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=path %>/dwr/interface/DWRMethod.js"></script>
<script type="text/javascript">
var functions = [
function(data){return data.stuId;},
function(data){return data.stuName;},
function(data){return data.stuAge},
function(data){return data.stuAddress}
];
function sendMessage() {
DWRMethod.getStudents();
}
function clientFunction(returnStr){
//document.all.view.value="已经广播给所有看到这个页面的用户了"+returnStr
DWRUtil.removeAllRows("mytbody");
DWRUtil.addRows("mytbody",returnStr,functions);
}
window.onload = function(){
dwr.engine.setActiveReverseAjax(true);
sendMessage();
}
</script>
</head>

<body>
<table id="mytab">
<thead>
<tr>
<td>
编号
</td>
<td>
姓名
</td>
<td>
年龄
</td>
<td>
家庭地址
</td>
</tr>
</thead>
<tbody id="mytbody">
</tbody>
</table>
<input id="inputButton" type="button" value="访问DWR" onclick="sendMessage()" />
</body>
</html>


代码大概就是这些了
现在出现的问题是:
当首次进入到页面时,能访问到我的业务方法getStudents();
地址是:http://localhost:8080/DWRDemo/dwr/call/plaincall/DWRMethod.getStudents.dwr
然后观察FF的控制台,当访问一次我的业务方法后,服务器一直都在访问
http://localhost:8080/DWRDemo/dwr/call/plainpoll/ReverseAjax.dwr
请问这是什么个情况,请指点,谢谢!!!


















...全文
487 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧