官方新版本有关于此clock的例子,但试验时还有问题;
从网上找到此例子,并试验能成功(感谢原作者),将结果放出,供需要者参考:
提示:dwr更新较快,若您下载的版本不能运行,可能是版本不兼容。
1、Clock.java:
package com.example.dwr.reverseajax;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.directwebremoting.Browser;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.ui.dwr.Util;
public class Clock implements Runnable {
protected transient boolean active = false;
int i = 0;
public Clock() {
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(
1);
executor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS);
}
public void run() {
if (active) {
setClockDisplay((new java.text.SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒")).format(new Date()));
}
}
/**
* Called from the client to turn the clock on/off
*/
public synchronized void toggle() {
i++;
active = !active;
System.out.println("toggle clicked!" + i);
System.out.println("active:" + active);
if (active) {
setClockDisplay("Started");
} else {
setClockDisplay("Stopped");
}
}
public void setClockDisplay(final String output) {
String page = ServerContextFactory.get().getContextPath() + "/test.jsp";
Browser.withPage(page, new Runnable() {
public void run() {
Util.setValue("clockDisplay", output);
}
});
}
}
2、web.xml,注意项目还需引入commons-logging包:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<description>是否激活反向Ajax</description>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<description>在WEB启动时是否创建范围为application的creator</description>
<param-name>initApplicationScopeCreatorsAtStartup</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
3、dwr.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">
<dwr>
<allow>
<create creator="new" scope="application">
<param name="class" value="com.example.dwr.reverseajax.Clock"/>
</create>
</allow>
</dwr>
4、测试页test.jsp:
<!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>
<%
String path = request.getContextPath();
%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Server-side clock</title>
<script type='text/javascript' src="<%=path %>/dwr/interface/Clock.js"></script>
<script type="text/javascript" src="<%=path %>/dwr/engine.js">
</script>
<script type="text/javascript" src="<%=path %>/dwr/util.js">
</script>
</head>
<body οnlοad="dwr.engine.setActiveReverseAjax(true);">
点击查看服务器端时间:
<input type="button" value="Start/Stop" οnclick="Clock.toggle();"/>
<h2 id="clockDisplay"></h2>
</body>
</html>