无论是传统的web开发还是上面提到的Ajax技术,在client与server数据交换中,都是client首先发起pull动作,之后,server给以响应,而若我们期待server端的变化比如server是监控某台主机的运行情况能够实时的主动反馈到client,那我们就要考虑Reverse Ajax了,它能够使得server主动的push数据到client,接下来我们以clock时钟程序为例,演示一下Reverse Ajax技术: A.开发server端程序Clock
package org.twolf.dwrdemo.clock;
public class Clock implements Runnable { public Clock() { ServletContext servletContext = WebContextFactory.get().getServletContext(); sctx = ServerContextFactory.get(servletContext); } public synchronized void toggle() { active = !active; if (active) { new Thread(this).start(); } }
/* * @see java.lang.Runnable#run() */ public void run() { try { log.debug("CLOCK: Starting server-side thread");
while (active) { Collection sessions = sctx.getScriptSessionsByPage("/dwr/clock/index.html"); Util pages = new Util(sessions); pages.setValue("clockDisplay", new Date().toString());
log.debug("Sent message"); Thread.sleep(1000); }
Collection sessions = sctx.getScriptSessionsByPage("/dwr/clock/index.html"); Util pages = new Util(sessions); pages.setValue("clockDisplay", "");
log.debug("CLOCK: Stopping server-side thread"); } catch (InterruptedException ex) { ex.printStackTrace(); } }
/** * Our key to get hold of ServerContexts */ private ServerContext sctx;
/** * Are we updating the clocks on all the pages? */ private transient boolean active = false; private static final Logger log = Logger.getLogger(Clock.class); } B.配置dwr.xml 向该配置文件中加入下面的配置信息: <!-- clock --> <create creator="new" javascript="Clock" scope="application"> <param name="class" value="org.twolf.dwrdemo.clock.Clock"/> C.书写client端 如上图1所示,我们需要完成WebRoot/clock目录下index.html文件的代码书写: index.html: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Server Side Reverse Ajax Clock</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type='text/javascript' src='/dwr/dwr/interface/Clock.js'> </script> <script type='text/javascript' src='/dwr/dwr/engine.js'> </script> <script type='text/javascript' src='/dwr/dwr/util.js'> </script> </head> <body onload="dwr.engine.setActiveReverseAjax(true);"> <h1>Server Side Reverse Ajax Clock</h1> <p>Creating a clock in a web page is easy, but what about a clock controlled by the server? This demo shows how use use a separate server side thread to control a number of browsers.</p> <input type="button" value="Start / Stop" onclick="Clock.toggle();"/> <p></p> <div style="font-size:200%;" id="clockDisplay"></div> </body> </html>
至此,重新部署该工程,启动tomcat,便可运行本程序,如下图3所示:
图 3 5、小结 A.大致开发流程 1.编写业务代码,该代码是和dwr无关的 4.配置dwr组件到dwr.xml文件中,如果有必要,配置convert, 进行java和javascript类型互转 #Q log4j:WARN No appenders could be found for logger (org.directwebremoting.util.Logger). log4j:WARN Please initialize the log4j system properly. #A 没有加载log4j #Q #Q [org.directwebremoting.dwrp.DefaultConverterManager]-[INFO] Probably not an issue: org.jdom.Document is not available so the jdom converter will not load. This is only an problem if you wanted to use it. #A 缺少jdom.jar包
四、EXTJS 应用举例
|
WEB开发之旅—站在山脚下【续 2】
最新推荐文章于 2024-09-10 06:15:30 发布
WEB开发之旅—站在山脚下【续 2】