DWR 3.0 结合EasyUI实现实时数据动态刷新
Easy for ajax
需求:结合EasyUI实现实时数据动态刷新
目的:使用dwr反向ajax原理,减少前端主动请求连接,由后台定时推送数据刷新,减少服务器请求压力
1.使用环境及jar包依赖
-
pom.xml 添加
org.directwebremoting dwr 3.0.2-RELEASE commons-logging commons-logging 1.2
- WEB-INFO 下创建 dwr.xml
//对应前端使用的 LogManager.js //LogManager为调用类
- 添加dwr访问路径至web.xml
Direct Web Remoter Servlet DWR Servlet dwr-invoker org.directwebremoting.servlet.DwrServlet debug true pollAndCometEnabled true 1 dwr-invoker /dwr/*
目录结构如下:
-
NodeInfoDWR类
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;import com.oss.model.NodeInfo;public class NodeInfoDWR {private FreshNodeInfo fresh;//结束线程public void stop() {if (fresh != null) {fresh.halt();}}//开启刷新数据线程public void init(String str){Map map = JsonKit.parse(str,Map.class);stop();WebContext wctx = WebContextFactory.get();ScriptSession scriptSession = wctx.getScriptSession();fresh = new FreshNodeInfo(scriptSession,map);fresh.start();}}class FreshNodeInfo extends Thread{private boolean active = true;private ScriptSession scriptSession;private Map map;public FreshNodeInfo(ScriptSession scriptSession,Map map) {this.scriptSession = scriptSession;this.map = map;}@Overridepublic void run() {while(active){List list = NodeInfo.dao.findAll();ScriptBuffer scriptBuffer = new ScriptBuffer();scriptBuffer.appendCall("freshNodeInfo", list);scriptSession.addScript(scriptBuffer);try {TimeUnit.SECONDS.sleep(5); // 定时5秒} catch (InterruptedException e) {e.printStackTrace();}}}public void halt(){this.active = false;}}
-
前端界面引入相关js
PS:如有雷同,纯属巧合
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

