DWR Reverse Ajax

來源:互聯網
上載者:User

1. 類

 

1.1 handle類

 

package com.cloverworxs.uma.helper;<br />import java.util.Collection;<br />import java.util.LinkedList;<br />import org.directwebremoting.WebContext;<br />import org.directwebremoting.WebContextFactory;<br />import org.directwebremoting.proxy.dwr.Util;<br />public class DWRHelper {</p><p>private static final LinkedList messages = new LinkedList();</p><p> public static void addMessage(String text) {</p><p> if (text != null && text.trim().length() > 0) {<br /> messages.addFirst(new Message(text));<br /> while (messages.size() > 5) {<br /> messages.removeLast();<br /> }<br /> }<br /> // Reverse Ajax code to be added here shortly<br /> WebContext wctx = WebContextFactory.get();<br /> String currentPage = wctx.getCurrentPage();</p><p> // Clear the input box in the browser that kicked off this page only<br /> Util utilThis = new Util(wctx.getScriptSession());<br /> utilThis.setValue("text", "");<br /> // For all the browsers on the current page:<br /> Collection sessions = wctx.getScriptSessionsByPage(currentPage);<br /> Util utilAll = new Util(sessions);<br /> // Clear the list and add in the new set of messages<br /> utilAll.removeAllOptions("chatlog");<br /> utilAll.addOptions("chatlog", messages, "text");<br /> utilAll.addFunctionCall("changeStyle");<br /> }</p><p>}<br />

 

1.2 bean類

package com.cloverworxs.uma.helper;<br />import org.directwebremoting.Security;<br />public class Message {<br /> public Message(String newtext) {<br /> text = newtext;<br /> if (text.length() > 256) {<br /> text = text.substring(0, 256);<br /> }<br /> text = Security.replaceXmlCharacters(text);<br /> }<br /> public long getId() {<br /> return id;<br /> }<br /> public String getText() {<br /> return text;<br /> }<br /> private long id = System.currentTimeMillis();<br /> private String text;<br />}<br />

 

2. web.xml

 

<?xml version="1.0" encoding="ISO-8859-1" ?><br /><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"><br /><web-app><br /> <display-name>Ajax Upload</display-name><br /> <servlet><br /> <servlet-name>dwr-invoker</servlet-name><br /> <display-name>DWR Servlet</display-name><br /> <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class><br /> <init-param><br /> <param-name>debug</param-name><br /> <param-value>false</param-value><br /> </init-param><br /> <init-param><br /> <param-name>logLevel</param-name><br /> <param-value>WARN</param-value><br /> </init-param><br /> <!-- activeReverseAjaxEnabled important --><br /> <init-param><br /> <param-name>activeReverseAjaxEnabled</param-name><br /> <param-value>true</param-value><br /> </init-param><br /> <load-on-startup>1</load-on-startup><br /> </servlet><br /> <servlet-mapping><br /> <servlet-name>dwr-invoker</servlet-name><br /> <url-pattern>/dwr/*</url-pattern><br /> </servlet-mapping><br /> <!-- The Usual Welcome File List --><br /> <welcome-file-list><br /> <welcome-file>index.jsp</welcome-file><br /> </welcome-file-list><br /></web-app><br />

3. dwr.xml

 

<dwr><br /> <allow></p><p> <create creator="none" javascript="DWRHelper"><br /> <param name="class" value="com.cloverworxs.uma.helper.DWRHelper" /><br /> <include method="addMessage" /><br /> </create></p><p> <convert converter="bean" match="com.cloverworxs.uma.helper.Message"><br /> <param name="include" value="id,text" /><br /> </convert></p><p> </allow><br /></dwr><br />

 

4. index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"<br /> pageEncoding="UTF-8"%><br /><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><br /><html><br /><head><br /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br /><title>Insert title here</title><br /><mce:script type='text/javascript' src="dwr/interface/DWRHelper.js" mce_src="dwr/interface/DWRHelper.js"></mce:script><br /><mce:script type='text/javascript' src="dwr/engine.js" mce_src="dwr/engine.js"></mce:script><br /><mce:script type='text/javascript' src="dwr/util.js" mce_src="dwr/util.js"></mce:script><br /><mce:script type="text/javascript"><!--<br /> function sendMessage() {</p><p> DWRHelper.addMessage(dwr.util.getValue("text"));</p><p> }</p><p> function changeStyle() {</p><p> if($('chatlog').style.listStyleType == 'decimal'){<br /> $('chatlog').style.listStyleType = 'upper-alpha';<br /> }else{<br /> $('chatlog').style.listStyleType = 'decimal';<br /> }</p><p> }<br />// --></mce:script><br /></head><br /><!-- dwr.engine.setActiveReverseAjax(true) important --><br /><body onload="dwr.engine.setActiveReverseAjax(true);"><br /><p>This is a very simple chat demo that uses reverse ajax to collect messages and server-side browser manipulation to update the pages with the results.</p><br /><p>Your Message: <input id="text" onkeypress="dwr.util.onReturn(event, sendMessage)" /> <input type="button" value="Send" onclick="sendMessage()" /></p><br /><hr /><br /><ul id="chatlog" style="list-style-type:armenian;" mce_style="list-style-type:armenian;"><br /></ul><br /></body><br /></html>

 

5. 從非DWR的servlet線程使用 Reverse Ajax

 

List containers = ContainerUtil.getAllPublishedContainers(getServletContext());<br />DefaultContainer container = (DefaultContainer)containers.get(0);<br /> ScriptSessionManager sessionManager = (ScriptSessionManager) container.getBean(ScriptSessionManager.class.getName());<br />// 群發<br /> //Collection sessions = sessionManager.getScriptSessionsByPage(request.getContextPath()+File.separator+"jsp/import/import.jsp");</p><p> // 單發,頁面通過類似uploadForm.action = "<%=strContextPath%>/servlet/FileUpload?scriptSessionId="+dwr.engine._getScriptSessionId() 形式發送scriptSessionId參數<br /> String id = (String) request.getParameter("scriptSessionId");<br /> RealScriptSession mySession= sessionManager.getScriptSession(id);<br /> sessionManager.setPageForScriptSession(mySession, request.getContextPath()+File.separator+"jsp/import/import.jsp");

 

6. 注意事項

 

6.1 需要的類庫 dwr.jar    commons-logging-1.1.1.jar   log4j-1.2.8.jar

6.2 log4j 設定檔

 

#--------------------------------<br /># LEVEL = FATALERRORWARNINFODEBUG<br />log4j.rootLogger=INFO, stdout, fileout </p><p>log4j.appender.stdout=org.apache.log4j.ConsoleAppender </p><p>log4j.appender.fileout=org.apache.log4j.RollingFileAppender<br />log4j.appender.fileout.File=/home/guo/ibatis.log<br />log4j.appender.fileout.MaxFileSize=10000KB </p><p>log4j.appender.stdout.layout=org.apache.log4j.PatternLayout<br />log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} :%m%n<br />log4j.appender.fileout.layout=org.apache.log4j.PatternLayout<br />log4j.appender.fileout.layout.ConversionPattern=[%-5p]_%d{yyyy-MM-dd HH:mm:ss} :%m%n </p><p>

6.3 dwr.xml檔案要已以下內容開頭(注意版本號碼)

 

<!DOCTYPE dwr PUBLIC<br /> "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"<br /> "http://getahead.org/dwr/dwr30.dtd">

 

6.4 MSXML5.0.DLL問題建議升級到3.0使用。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.