大家帮我看下,这是我用struts写的一个程序,我现在想加一个实时刷新的功能,意思就是当我的数据库中有变法时才刷新,而数据库中没有发生变化时就不需要刷新。。请大家帮帮忙,看看该怎么改。
另外我一开始启动的是Del.jsp
我把数据库取出来的东西是存在一个StringBuffer里面,然后再以xml格式存储(页面代码在Del1.jsp)。我现在就是想如何用js控制下,也就是判断下xml更新与否来进行刷新页面。大家有好的建议或解决方法麻烦告诉下我。Del.jsp<%@ page language="java" pageEncoding="GBK"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base /> <title>Del.jsp</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">
var xmlHttp;
var id;
function trim(str)
{
var t = str.replace(/(^\s*)|(\s*$)/g, ""); // 用正则表达式将前后空格
return t.replace(/(^ *)|( *$)/g, ""); // 用空字符串替代。
} function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}
function validate(){
createXMLHttpRequest();
//var url="Del1.jsp";
var url = "/jixiao/conn.do"
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=callback;
xmlHttp.send(null);
}
function callback(){
if(xmlHttp.readyState==4){
show();
}
}
function Start(ele){
// alert(ele.id);
id=ele.id;
createXMLHttpRequest();
var url="/jixiao/conn.do?key="+escape(ele.id);
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=Del;
xmlHttp.send(null);
}
function Del(){
if(xmlHttp.readyState==4){
var ss=xmlHttp.responseText;
// alert("该项已被删除,1秒后会自动显示最新数据");
setTimeout("validate()",5000);
// alert(ss);
}
}
function show(){
var xmlDoc=xmlHttp.responseXML;
var yan=xmlDoc.getElementsByTagName("content");
var ta = "<table border=1 cellspacing=0 width=35% align=center id=ta><tr><td align=center>任务名称</td><td align=center>执行人名称</td><td align=center>开始时间</td><td align=center>完成时间</td></tr>";
for(var i=0;i<yan.length;i++){
var y=yan[i];
var taskname=y.childNodes[0].firstChild.data;
var executename=y.childNodes[1].firstChild.data;
var begintime=y.childNodes[2].firstChild.data;
var endtime=y.childNodes[3].firstChild.data;
ta+="<tr id='aa'><td width=15% align=center>"+taskname+"</td>";
ta+="<td width=15% align=center>"+executename+"</td>";
ta+="<td width=15% align=center>"+begintime+"</td>";
ta+="<td width=15% align=center>"+endtime+"</td>";
//ta+="<td><p id="+taskname+"style='color:green;' onclick='Start(this)'><u>删除</u></p></td></tr>";
}
ta+="</table>";
//alert(yan.length);
document.getElementById("res").innerHTML=ta;
}
</script></head><body onload="validate()">
<h3 align=center>
显示页面
</h3>
<div id="res"></div>
</body>
</html:html>
Del1.jsp<%@ page contentType="text/html; charset=utf-8" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*" %>
<%
try {
StringBuffer content=new StringBuffer("");
ArrayList<ResultSet> list = (ArrayList<ResultSet>)request.getAttribute("rslist");//getRs("select * from user1");
ResultSet rs = list.get(0);
response.setContentType("text/xml;charset=utf-8");
response.setHeader("Cache-Control","no-cache");
content.append("<contents>\r\n");
while (rs.next()) {
String name = rs.getString(1);
String executename = rs.getString(2);
String begintime = rs.getString(3);
String endtime = rs.getString(4);
content.append("<content>\r\n");
content.append("<name>\r\n" + name + "\r\n</name>\r\n");
content.append("<executename>\r\n" + executename + "\r\n</executename>\r\n");
content.append("<begintime>\r\n" + begintime + "\r\n</begintime>\r\n");
content.append("<endtime>\r\n" + endtime + "\r\n</endtime>\r\n");
content.append("</content>\r\n");
}
content.append("</contents>\r\n");
out.print(content); } catch (Exception e) { e.printStackTrace();
}
%>
ConnAction.java
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.shangluo.struts.action;import java.sql.ResultSet;
import java.util.ArrayList;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;import com.shangluo.dao.ConnDao;/**
* MyEclipse Struts Creation date: 09-04-2008
*
* XDoclet definition:
*
* @struts.action validate="true"
*/
public class ConnAction extends Action {
/*
* Generated Methods
*/ /**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String key = request.getParameter("key");
String sql = "";
ResultSet rs = null;
ArrayList<ResultSet> list = new ArrayList<ResultSet>();
try {
sql="select t.task_name, r.res_name, a.assn_start_date,a.assn_finish_date from msp_tasks t ,msp_resources r, msp_assignments a where a.res_uid=r.res_id and a.task_uid=t.task_uid order by a.assn_finish_date";
list = new ConnDao().getRs(sql);//里面的方法就是查询下数据库,得到一个结果集,没其他方法。数据库是oracle
request.setAttribute("rslist", list);
} catch (Exception e) {
e.printStackTrace();
} return mapping.findForward("view");
}
}struts-config.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"><struts-config>
<form-beans />
<global-exceptions />
<global-forwards />
<action-mappings >
<action path="/conn" type="com.shangluo.struts.action.ConnAction">
<forward name="view" path="/Del1.jsp" />
</action> </action-mappings> <message-resources parameter="com.shangluo.struts.ApplicationResources" />
</struts-config>
另外我一开始启动的是Del.jsp
我把数据库取出来的东西是存在一个StringBuffer里面,然后再以xml格式存储(页面代码在Del1.jsp)。我现在就是想如何用js控制下,也就是判断下xml更新与否来进行刷新页面。大家有好的建议或解决方法麻烦告诉下我。Del.jsp<%@ page language="java" pageEncoding="GBK"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base /> <title>Del.jsp</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">
var xmlHttp;
var id;
function trim(str)
{
var t = str.replace(/(^\s*)|(\s*$)/g, ""); // 用正则表达式将前后空格
return t.replace(/(^ *)|( *$)/g, ""); // 用空字符串替代。
} function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}
function validate(){
createXMLHttpRequest();
//var url="Del1.jsp";
var url = "/jixiao/conn.do"
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=callback;
xmlHttp.send(null);
}
function callback(){
if(xmlHttp.readyState==4){
show();
}
}
function Start(ele){
// alert(ele.id);
id=ele.id;
createXMLHttpRequest();
var url="/jixiao/conn.do?key="+escape(ele.id);
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=Del;
xmlHttp.send(null);
}
function Del(){
if(xmlHttp.readyState==4){
var ss=xmlHttp.responseText;
// alert("该项已被删除,1秒后会自动显示最新数据");
setTimeout("validate()",5000);
// alert(ss);
}
}
function show(){
var xmlDoc=xmlHttp.responseXML;
var yan=xmlDoc.getElementsByTagName("content");
var ta = "<table border=1 cellspacing=0 width=35% align=center id=ta><tr><td align=center>任务名称</td><td align=center>执行人名称</td><td align=center>开始时间</td><td align=center>完成时间</td></tr>";
for(var i=0;i<yan.length;i++){
var y=yan[i];
var taskname=y.childNodes[0].firstChild.data;
var executename=y.childNodes[1].firstChild.data;
var begintime=y.childNodes[2].firstChild.data;
var endtime=y.childNodes[3].firstChild.data;
ta+="<tr id='aa'><td width=15% align=center>"+taskname+"</td>";
ta+="<td width=15% align=center>"+executename+"</td>";
ta+="<td width=15% align=center>"+begintime+"</td>";
ta+="<td width=15% align=center>"+endtime+"</td>";
//ta+="<td><p id="+taskname+"style='color:green;' onclick='Start(this)'><u>删除</u></p></td></tr>";
}
ta+="</table>";
//alert(yan.length);
document.getElementById("res").innerHTML=ta;
}
</script></head><body onload="validate()">
<h3 align=center>
显示页面
</h3>
<div id="res"></div>
</body>
</html:html>
Del1.jsp<%@ page contentType="text/html; charset=utf-8" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*" %>
<%
try {
StringBuffer content=new StringBuffer("");
ArrayList<ResultSet> list = (ArrayList<ResultSet>)request.getAttribute("rslist");//getRs("select * from user1");
ResultSet rs = list.get(0);
response.setContentType("text/xml;charset=utf-8");
response.setHeader("Cache-Control","no-cache");
content.append("<contents>\r\n");
while (rs.next()) {
String name = rs.getString(1);
String executename = rs.getString(2);
String begintime = rs.getString(3);
String endtime = rs.getString(4);
content.append("<content>\r\n");
content.append("<name>\r\n" + name + "\r\n</name>\r\n");
content.append("<executename>\r\n" + executename + "\r\n</executename>\r\n");
content.append("<begintime>\r\n" + begintime + "\r\n</begintime>\r\n");
content.append("<endtime>\r\n" + endtime + "\r\n</endtime>\r\n");
content.append("</content>\r\n");
}
content.append("</contents>\r\n");
out.print(content); } catch (Exception e) { e.printStackTrace();
}
%>
ConnAction.java
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.shangluo.struts.action;import java.sql.ResultSet;
import java.util.ArrayList;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;import com.shangluo.dao.ConnDao;/**
* MyEclipse Struts Creation date: 09-04-2008
*
* XDoclet definition:
*
* @struts.action validate="true"
*/
public class ConnAction extends Action {
/*
* Generated Methods
*/ /**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String key = request.getParameter("key");
String sql = "";
ResultSet rs = null;
ArrayList<ResultSet> list = new ArrayList<ResultSet>();
try {
sql="select t.task_name, r.res_name, a.assn_start_date,a.assn_finish_date from msp_tasks t ,msp_resources r, msp_assignments a where a.res_uid=r.res_id and a.task_uid=t.task_uid order by a.assn_finish_date";
list = new ConnDao().getRs(sql);//里面的方法就是查询下数据库,得到一个结果集,没其他方法。数据库是oracle
request.setAttribute("rslist", list);
} catch (Exception e) {
e.printStackTrace();
} return mapping.findForward("view");
}
}struts-config.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"><struts-config>
<form-beans />
<global-exceptions />
<global-forwards />
<action-mappings >
<action path="/conn" type="com.shangluo.struts.action.ConnAction">
<forward name="view" path="/Del1.jsp" />
</action> </action-mappings> <message-resources parameter="com.shangluo.struts.ApplicationResources" />
</struts-config>
数据就是3张简单的表,没什么的
我那个Dao里面就是得到一个ResultSet结果集,然后把ResultSet放到list返回就没了
<script>
var ajax = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Msxml2.XMLHTTP")
function getMsg()
{
ajax.open("GET","dosomething.action?tmp=" + Date.parse(new Date()),true)
ajax.onreadystatechange = callback;
ajax.setRequestHeader("Connection", "close");
ajax.send(null);
}function callback()
{
if(ajax.readyState==4)
{
if(ajax.status==200)
{
document.getElementById("msg").innerHTML = ajax.responseText;
}
}
}
window.setInterval("getMsg()",5000)
</script>dosomething.action里面,将新数据进行转换成html返回即可