DWR Reverse Ajax

Source: Internet
Author: User

1. Class

 

1.1 handle class

 

Package com. cloverworxs. uma. helper; <br/> import java. util. collection; <br/> import java. util. export list; <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 sorted list messages = new sorted list (); </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/> // 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 class

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 () & gt; 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 // "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> d Ebug </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> <create creator = "none" javascript = "DWRHelper"> <br/> <param name = "class "value =" com. cloverworxs. uma. helper. DWRHelper "/> <br/> <include method =" addMessage "/> <br/> </create> </p> <convert converter =" bean "match = "com. cloverworxs. uma. helper. message "> <br/> <param name =" include "value =" id, text "/> <br/> </convert> </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/> <ptml> <br/> <pead> <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's Rc = "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/> </pead> <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/> <pr/> <br/> <ul id =" chatlog "style =" list-style-type: armenian; "mce_style =" list-style-type: armenian; "> <br/> </ul> <br/> </body> <br/> </ptml>

 

5. Use reverse Ajax from non-DWR servlet threads

 

List containers = containerutil. getallpublishedcontainers (getservletcontext (); <br/> defaultiner iner Container = (defaultcontainer) containers. get (0); <br/> scriptsessionmanager sessionmanager = (scriptsessionmanager) container. getbean (scriptsessionmanager. class. getname (); <br/> // group <br/> // collection sessions = sessionmanager. getscriptsessionsbypage (request. getcontextpath () + file. separator + "JSP/I Mport/import. jsp "); </P> <p> // single shot. The page is displayed using a method similar to uploadform. Action =" <% = strcontextpath %>/servlet/fileupload? Scriptsessionid = "+ DWR. engine. send the scriptsessionid parameter in the form of _ getscriptsessionid () <br/> string id = (string) request. getparameter ("scriptsessionid"); <br/> realscriptsession mysession = sessionmanager. getscriptsession (ID); <br/> sessionmanager. setpageforscriptsession (mysession, request. getcontextpath () + file. separator + "JSP/import. JSP ");

 

6. Notes

 

6.1 The required class library DWR. Jar commons-logging-1.1.1.jar log4j-1.2.8.jar

6.2 log4j configuration file

 

# -------------------------------- <Br/> # level = fatalerrorwarninfodebug <br/> log4j. rootlogger = info, stdout, fileout </P> <p> log4j. appender. stdout = org. apache. log4j. leleappender </P> <p> log4j. appender. fileout = org. apache. log4j. rollingfileappender <br/> log4j. appender. fileout. file =/home/Guo/ibatis. log <br/> log4j. appender. fileout. maxfilesize = 0000kb </P> <p> log4j. appender. stdout. layout = org. apache. log4j. patternlayout <br/> log4j. appender. stdout. layout. conversionpattern = [%-5 p] % d {yyyy-mm-dd hh: mm: SS }:% M % n <br/> log4j. appender. fileout. layout = org. apache. log4j. patternlayout <br/> log4j. appender. fileout. layout. conversionpattern = [%-5 p] _ % d {yyyy-mm-dd hh: mm: SS }:% M % n </P> <p>

6.3 The DWR. xml file must start with the following content (note the version number)

 

<! 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 problems are recommended to be upgraded to 3.0.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.