日期:2014-05-17 浏览次数:20767 次
1. public class SendPushService {
2. //发送消息
3. public void send(String msg) {
4. System.out.println("==========调用了send方法==========");
5. ScriptBuffer scriptBuffer = new ScriptBuffer(); //构造js脚本
6. WebContext webContext=WebContextFactory.get();
7. ScriptSession myScSession = webContext.getScriptSession();
8. scriptBuffer.appendScript("dwrtest(");
9. scriptBuffer.appendData(msg);
10. scriptBuffer.appendScript(")");
11. Util util = new Util(myScSession);
12. util.addScript(scriptBuffer); //向客户端推送消息
13. }
14. }
1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3. <html xmlns=" http://www.w3.org/1999/xhtml">
4. <head>
5. <base href="${basePath }" />
6. <script type='text/javascript' src='${basePath}dwr/engine.js'></script>
7. <script type='text/javascript' src='${basePath}dwr/util.js'></script>
8. <script type='text/javascript' src='${basePath}dwr/interface/SendPushService.js'></script>
9. <script type="text/javascript">
10. function hello(){
11. SendPushService.send("第一个dwr推程序");
12. }
13. /**由dwr在后台调用这个方法**/
14. function dwrtest(data){
15. alert(data);
16. }
17. </script>
18. <title>第一个dwr推程序</title>
19. </head>
20. <body onload="dwr.engine.setActiveReverseAjax(true);">
21. <input type="button" value="点击我" onclick="hello();" />