这次的项目要求有一个养殖池的监测指标的动态展现,就是在系统的主页,展示出各个养殖池的实时动态数据。
数据由另外的一段程序,实时从硬件读取写进数据库,所以要做的就是定时去数据库去取数据就行了。
贴出来的代码,主要是实现的页面展示部分:
第一次载入页面后,养殖池读取一次数据,然后开始调用Ext.TaskMgr来实现动态的去发起请求
- var
task_CheckLoginState;//声明任务变量 -
task_RealTimeMointor = { -
run : RealTimeMointor,//执行任务时执行的函数 -
interval : 10000 -
//任务间隔,毫秒为单位,这里是10秒 -
} -
//Ext.TaskMgr.start(task_CheckLoginState);//初始化时就启动任务 -
//定时启动执行一次,启动TaskMgr, -
begin = setInterval("Ext.TaskMgr.start(task_RealTimeMointor)",5000); //1000毫秒=1秒 -
function stopMonitor() -
{ -
Ext.TaskMgr.stop(task_RealTimeMointor); -
}
这就可以定时的调用RealTimeMointor函数 去执行发起请求
- function
RealTimeMointor() { -
clearInterval(begin);//关闭setInterval(); -
Ext.Ajax.request({ -
url: 'realtimemonitor_realtimeMonitorByWorkerS hopId.action', -
disableCaching: true,//禁止缓存 -
timeout: 60000,//最大等待时间,超出则会触发超时 -
success: function(response, option) -
{ -
if (!response || response.responseText == '') {//返回的内容为空,即服务器停止响应时 -
Ext.TaskMgr.stop(task_CheckLoginState); -
Ext.MessageBox.show({ -
title: '错误', -
msg: '在读取动态数据时返回了空数据,请确认您的设备问题,请重启浏览器再试!', -
buttons: Ext.Msg.OK, -
icon: Ext.MessageBox.ERROR, -
fn: function(btn, text){ -
if (btn == 'ok') { -
-
} -
} -
}); -
return; -
} -
else{ -
var respText = Ext.util.JSON.decode(response.responseText); //解码JSON格式数据为一个对象 -
//alert("daxiao==="+respText.info.length); -
insertIntoHtml(respText); -
//allPrpos(respText.info[1].monitorDetailInfoMap); -
//Ext.Msg.alert('提示', respText.info[1].monitorDetailInfoMap); -
} -
}, -
//请求失败时 -
failure: function(data){ -
Ext.MessageBox.show({ -
title: '错误', -
msg: '在读取动态数据时时发生网络错误,请确认您已经链接网络后,重启浏览器再试!', -
buttons: Ext.Msg.OK, -
icon: Ext.MessageBox.ERROR, -
fn: function(btn, text){ -
if (btn == 'ok') { -
} -
} -
}); -
}, -
//带的参数 -
params: { workShopId: tempWorkShopId } -
}); -
}
这个函数的url是请求数据的地址,返回的是一个json格式的数据,如何做请参照我的另外一篇文章
如何生成树
struts.xml
- <</span>package
name="edu.ldu.water.realtimemonitor.Action" extends="struts-default"> -
<</span>action name="realtimemonitor_*" class="RealTimeMonitorAction" -
method="{1}"> -
<</span>result name="showAllPool">/realtime/realtimepool.jsp</</span>result> -
<</span>result name="showAllPoolByAjax">/realtime/jsonaccepter.jsp</</span>result> -
</</span>action> - </</span>package>
一定要注意一点就是 ,一定要写个页面接一下你要取的数据,这样才能知道你要转哪个JSON。
/realtime/jsonaccepter.jsp