---恢复内容开始---
准备公工作
1.去官网下载jar和war
开发工具
eclipse
开始开发:gogogo
1:建立一个可以跑起来的javaweb项目,最基本的就可以了.
2:导入commons-logging-1.0.4.jar,dwr.jar
3:在web.xml里配置一下
<?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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Girl_dwr</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>dwr</servlet-name> <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class> <init-param> <param-name>activeReverseAjaxEnabled</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>corssDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>allowScriptTagRemoting</param-name> <param-value>true</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr</servlet-name> <url-pattern>/js/dwr/*</url-pattern> </servlet-mapping> </web-app>
4:配置dwr.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd"> <dwr> <allow> <create creator="new" javascript="DWRPush"> <param name="class" value="fz.song.girl.util.DWRPush"/> </create> </allow> </dwr>
5:配置一个java类DWRPush
package fz.song.girl.util; 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 DWRPush { public void send(String msg) { WebContext context = WebContextFactory.get(); Collection<ScriptSession> session = context.getAllScriptSessions(); ScriptBuffer sb = new ScriptBuffer(); sb.appendScript("callback("); sb.appendScript("""+msg+"""); sb.appendScript(")"); Util util = new Util(session); util.addScript(sb); } }
6:写一个jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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"> <script type="text/javascript" src="${pageContext.request.contextPath}/js/dwr/engine.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/js/dwr/util.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/js/dwr/interface/DWRPush.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.1.1.min.js"></script> <title>DWR_Test</title> <script type="text/javascript"> $(document).ready(function() { dwr.engine.setActiveReverseAjax(true); $("#sing").click(function() { var val = $("#message").val(); DWRPush.send(val); }); }); function callback(msg) { $("#ul").append("<li>"+msg+"</li>"); } </script> </head> <body> <ul id="ul" style="color: red; font-size: 60px"> </ul> <input type="text" id="message" name="message" size="30" style="height: 60px; font-size: 35px"> <input type="button" id="sing" value="发布信息"> </body> </html>
这里的几个坑要指出来一下;
在java类中那个msg要用引号包裹,否则会出问题.
jsp中那两个js和你的web.xml里的配置路劲一致.
就是这样,一个简单的demo就写好了.记下来,怕自己忘了.
---恢复内容结束---