有关dwr推送的笔记

时间:2023-01-20 05:35:06

想做一个web推送相关的东东,昨天搞了一天,终于把这些杂乱的配制弄清了,今天写出来方便以后记住,也方便大家看一下吧

1:引入dwr包,我用的是maven

<dependency>
<groupId>org.directwebremoting</groupId>
<artifactId>dwr</artifactId>
<version>3.0.M1</version>
</dependency>

2:配制web.xml

我是在springMVC下进行的

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>SmallSP Web Application</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:mvc/springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping> <filter>
<filter-name>Set Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

以上代码中添加的是以下这一段

<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<servlet-mapping>

3:配制springMVC.xml

在原有springMVC配制中添加以下

    <dwr:configuration />
<dwr:annotation-config id="dwr" />
<!-- 要求DWR将util.js和engine.js映射到dwrController -->
<dwr:url-mapping />
<!-- 定义dwr -->
<dwr:controller id="dwrController" debug="true">
<dwr:config-param name="allowScriptTagRemoting" value="true" />
<dwr:config-param name="crossDomainSessionSecurity" value="false" />
<dwr:config-param name="pollAndCometEnabled " value="true"/>
<dwr:config-param name="activeReverseAjaxEnabled" value="true"/>
<dwr:config-param name="debug " value="true"/>
</dwr:controller>

4:配制 RemoteProxy

@Controller
@RemoteProxy(name = "directController")
public class MyDwrController { @RemoteMethod
public String ShowHello(){
return "hello";
} @RemoteMethod
public void send(HttpServletRequest request,String msg){ ServletContext sc = request.getSession().getServletContext();
ServerContext sctx = ServerContextFactory.get(sc); //获得当前浏览 index.jsp 页面的所有脚本session
Collection<ScriptSession> sessions = sctx.getScriptSessionsByPage("/show"); Util util = new Util(sessions); ScriptBuffer sb = new ScriptBuffer();
sb.appendScript("show(");
sb.appendData(msg);
sb.appendScript(")"); //推送
util.addScript(sb); //util.addFunctionCall("show", ": " + msg);
//dwrtest为javascript函数
}
}

5:发送页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>123</title>
<script type='text/javascript' src='/static/jquery-min.js'></script>
<script type='text/javascript' src='<c:url value="/dwr/engine.js"/>'></script>
<script type='text/javascript' src='<c:url value="/dwr/util.js" />'></script>
<script type='text/javascript'
src='<c:url value="/dwr/interface/directController.js" />'></script>
<script type="text/javascript">
function view() {
directController.ShowHello(function(data) {
alert(data);
});
} function send(){
dwr.engine.setActiveReverseAjax(true);
directController.send($("#msg").val());
} </script>
</head>
<body> <input type="text" id="msg" />
<input type="button" value="发送" id="but" onclick="send();" /> </body>
</html>

6:接收页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type='text/javascript' src='/static/jquery-min.js'></script>
<script type='text/javascript' src='<c:url value="/dwr/engine.js"/>'></script>
<script type='text/javascript' src='<c:url value="/dwr/util.js" />'></script>
<%-- <script type='text/javascript'
src='<c:url value="/dwr/interface/directController.js" />'></script> --%> <script type="text/javascript">
$(function(){
//这句话千万不能少 ,表示允许使用推送技术
dwr.engine.setActiveReverseAjax(true);
});
//这个函数是提供给后台推送的时候 调用的
function show(msg){
$("#message").text(msg);
}
</script> </head>
<body>
<div id="message" style="width: 200px;height: 200px;border: 1px solid red ; text-align: center; padding: 5px;"></div>
</body>
</html>

总结:就是以上步骤了。