注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

love3400wind的blog

like

 
 
 

日志

 
 
 
 

dwr 后台推送技术(二)  

2012-11-30 15:30:07|  分类: IT |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
介绍:主要是通过dwr的推送技术
实例分为一个页面
Remote.java

package com.zdxa.test;

import java.util.Collection;

import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;



public class Remote {



@SuppressWarnings("unchecked")
public void sendMsg(String id, String context) {
WebContext wctx = WebContextFactory.get(); // 这里是获取WebContext上下文
String clientPage = "/项目名/callExe.jsp";// 推到哪个页面
// 要求的必须方式
Collection sessions = wctx.getScriptSessionsByPage(clientPage); // 在一个page中可能存在多个ScriptSessions,
Util utilAll = new Util(sessions); // Util 是DWR 在Server端模拟Brower端
String ss = context;
utilAll.setValue("aa", "this is aa value: " + ss);
utilAll.setStyle("bb", "display", "inline");
utilAll.setValue("bb", "<p> this is bb value, has already modeified display style. " + context + "</p>", false);
// ScriptBuffer sb = new ScriptBuffer();
utilAll.addFunctionCall("divShow", context);
}
}


 dwr.xml配置


<allow>
<create creator="new" javascript="Remote">
<param name="class" value="com.zdxa.test.Remote" />
<include method="sendMsg" />
</create>
</allow>


web.xml配置

<!-- 以下配置用于DWR -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>allowScriptTagRemoting</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>
<param-name>initApplicationScopeCreatorsAtStartup</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>maxWaitAfterWrite</param-name>
<param-value>100</param-value>
</init-param>
</servlet>

callExe.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ include file="/inc/comm.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'callExe.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="${pageContext.request.contextPath }/dwr/interface/Remote.js"></script>
<script language="JavaScript">


function clo (){
var obj = $("msgDIV");
obj.hide("slow");
}
function divShow(message) {
$("content").append("<div id='msgDIV' style='position: absolute;right: 2px;bottom: 20px;display:none;padding: 2px;width:200px;height: 200px;border: 1px solid black;'>");

$("msgDIV").append("<div id='close' style='cursor:hand' align='right' onclick='clo()'>close</div>");
$("msgDIV").append("<div id='msg' align='center'><font color='red'></font></div>");
$("msgDIV").append("<div align='right'><font color='bule'><a href='http://blog.163.com/love3400wind@163/blog/#'>click here to more</a></font></div>");
$("content").append("</div>");
$("msg").html("<p>"+message+"</p>");
$("msgDIV").show("slow");
}

function test() {
var msg = null;
msg = {msgId: '1', context: $("msgContext").value};
var msgObj = {};
msgObj.msgId = '1';
msgObj.context = $("msgContext").value;
Remote.sendMsg('1', $("msgContext").value);
}
</script>


</head>

<body>
This is my JSP page. <br>
<br>


ID: <input type="text" name="msgId" id="msgId" /> <br />
context : <input type="text" name="msgContext" id="msgContext" />
<input type="button" value="Send" onclick="test()" />
<br><br>
<div id="content">
<input type="text" name="aa" id="aa" />
<div id="bb" style="display: none; width: 300px; height: 300px">
</div>
</div>
<br>
</body>
</html>


inc/comm.jsp主要是包含公共的方法,截取一段

<script type="text/javascript" src="${pageContext.request.contextPath}/dwr/engine.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/dwr/util.js"></script>


  评论这张
 
阅读(730)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017