首页 >web前端 >html教程 >dwr进行页面推送简单实例_html/css_WEB-ITnose

dwr进行页面推送简单实例_html/css_WEB-ITnose

WBOY
WBOY原创
2016-06-24 11:50:44873浏览

实现功能:sendMsg.jsp发送一个消息到后台,后台将消息推送到showMsg.jsp页面。

1、web.xml 配置,在web.xml里面添加如下内容:

<!-- 使用DWR进行页面消息推送  -->	<servlet>		<display-name>DWR Servlet</display-name>		<servlet-name>dwr-invoker</servlet-name>		<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>		<init-param>			<param-name>debug</param-name>			<param-value>true</param-value>		</init-param>				<!-- 使用polling和comet的方式 -->		<init-param>			<param-name>pollAndCometEnabled</param-name>			<param-value>true</param-value>		</init-param>		<load-on-startup>1</load-on-startup>	</servlet>		<servlet-mapping>		<servlet-name>dwr-invoker</servlet-name>		<url-pattern>/dwr/*</url-pattern>	</servlet-mapping>

2、配置dwr.xml

<dwr>  <allow>  <!-- 这里的javascript 值就是页面引用的第三个js文件的名称-->    <create creator="new" javascript="SendMsg">      <param name="class">iwco.dwrs.SendMsg      </create>  </allow></dwr>

3、java类 SendMsg.java

package iwco.dwrs;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 SendMsg {	 @SuppressWarnings("deprecation")  	    public void sendMsg(String msg){	        //得到上下文  	        WebContext contex = WebContextFactory.get();  	          	        //得到要推送到 的页面  dwr3为项目名称 , 一定要加上。  	        Collection<scriptsession> sessions = contex.getScriptSessionsByPage("/dwrTest/showMsg.jsp");	          	        //不知道该怎么解释这个 ,   	        Util util = new Util(sessions);	          	        //下面是创建一个javascript脚本 , 相当于在页面脚本中添加了一句  show(msg);   	        ScriptBuffer sb = new ScriptBuffer();  	        sb.appendScript("show(");  	        sb.appendData(msg);  	        sb.appendScript(")");  	          	        //推送  	        util.addScript(sb);  	    }  }</scriptsession>

4、页面

sendMsg.jsp

      <base href="<%=basePath%>">    <title>sendMsg.jsp</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">		<script type="text/javascript" src="/dwrTest/dwr/util.js"></script>    	<script type="text/javascript" src="/dwrTest/dwr/engine.js"></script>  	<script type="text/javascript" src="/dwrTest/dwr/interface/SendMsg.js"></script>   	<script type="text/javascript" src="/dwrTest/js/jquery.min.js"></script>	<script type="text/javascript">      $(function(){          dwr.engine.setActiveReverseAjax(true);          $("#but").click(function(){            SendMsg.sendMsg($("#msg").val());          });      });  </script>         	 <input type="text" id="msg">      <input type="button" value="发送" id="but">    

showMsg.jsp

      <base href="<%=basePath%>">    <title>showMsg</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">	<script type="text/javascript" src="/dwrTest/dwr/util.js"></script>    	<script type="text/javascript" src="/dwrTest/dwr/engine.js"></script>  	<script type="text/javascript" src="/dwrTest/dwr/interface/SendMsg.js"></script>   	<script type="text/javascript" src="/dwrTest/js/jquery.min.js"></script>	<script type="text/javascript">      $(function(){          //这句话千万不能少 ,表示允许使用推送技术          dwr.engine.setActiveReverseAjax(true);      });      //这个函数是提供给后台推送的时候  调用的        function show(msg){          $("#message").text(msg);      }  </script>        <div id="message" style="width: 200px;height: 200px;border: 1px solid red ; text-align: center; padding: 5px;"></div>  


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn