1、首先是dwr的jar包引入(基于maven的pom文件引入)
<dependency> <groupId>org.directwebremoting</groupId> <artifactId>dwr</artifactId> <version>3.0.2-RELEASE</version> </dependency>
2、与web.xml同级创建dwr.xml文件,内容如下,注意:value路径为SendMessage这个java类的路径,javascript写为类名即可
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd"> <dwr> <!--java代码和js方法的映射--> <allow> <create creator="new" javascript="SendMessage"> <param name="class" value="xx.xx.xx.SendMessage" /> </create> </allow> </dwr>
3、web.xml中加入以下配置
<servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <description>调试DWR,发布系统时应设为false</description> <param-name>debug</param-name> <param-value>false</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> <init-param> <param-name>maxWaitAfterWrite</param-name> <param-value>100</param-value> </init-param> <load-on-startup>4</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
4、创建名为SendMessage的java类,内容如下
import org.directwebremoting.Browser; import org.directwebremoting.Container; import org.directwebremoting.ScriptBuffer; import org.directwebremoting.ScriptSession; import org.directwebremoting.ScriptSessionFilter; import org.directwebremoting.ServerContextFactory; import org.directwebremoting.WebContextFactory; import org.directwebremoting.event.ScriptSessionEvent; import org.directwebremoting.event.ScriptSessionListener; import org.directwebremoting.extend.ScriptSessionManager; import org.directwebremoting.servlet.DwrServlet; public class SendMessage { public void onPageLoad (String userId) { scriptSession = WebContextFactory.get().getScriptSession(); scriptSession.setAttribute("userId", userId); DwrScriptSessionManagerUtil dwrScriptSessionManager = new DwrScriptSessionManagerUtil(); dwrScriptSessionManager.init(); } public void sendMsg (String userid, String message, String jsFuc) { final String userId = userid; final String msg = message; final String jsFunc = jsFuc; Browser.withAllSessionsFiltered(new ScriptSessionFilter() { @Override public boolean match(ScriptSession scriptSession) { if (scriptSession.getAttribute("userId") == null) { return false; } else { return (scriptSession.getAttribute("userId")).equals(userId); } } }, new Runnable(){ private ScriptBuffer scriptBuffer = new ScriptBuffer(); public void run () { //推送消息 scriptBuffer.appendCall(jsFunc,msg); Collection<ScriptSession> scriptSessions = Browser.getTargetSessions(); for (ScriptSession scriptSession : scriptSessions) { scriptSession.addScript(scriptBuffer); } } }); } } class DwrScriptSessionManagerUtil extends DwrServlet { public void init () { Container container = ServerContextFactory.get().getContainer(); ScriptSessionManager manager = container.getBean(ScriptSessionManager.class); ScriptSessionListener listener = new ScriptSessionListener() { @Override public void sessionCreated(ScriptSessionEvent scriptSessionEvent) { HttpSession session = WebContextFactory.get().getSession(); String userId = "p1"; System.out.println("a message created!"); scriptSessionEvent.getSession().setAttribute("userId", userId); } @Override public void sessionDestroyed(ScriptSessionEvent scriptSessionEvent) { System.out.println("a message is destroyed!"); } }; manager.addScriptSessionListener(listener); } }
5、前端发送跟接收页面都额外引入如下三个文件,注意:SendMessage与dwr.xml中javascript值必须一样,路径跟.js不能改变
<script language='javascript' src='/dwr/engine.js'></script> <script language='javascript' src='/dwr/util.js'></script> <script language='javascript' src='/dwr/interface/SendMessage.js'></script>
6、前端发送跟接收页面都初始化如下三行代码
dwr.engine.setActiveReverseAjax(true);//激活反转 dwr.engine.setNotifyServerOnPageUnload(true); SendMessage.onPageLoad(”);
7、发送页面写好点击事件,内容如下
SendMessage.sendMsg('p1', '我是一条消息', 'receive');
8、接收页面写好receive方法,这个方法名与发送页面点击事件里面那行代码的第三个参数必须相同
这里可以function receive (msg) {
console.log(msg)
或者alert(msg)
}