dwr开发指南---Reverse Ajax

来源:互联网 发布:杜兰特2016季后赛数据 编辑:程序博客网 时间:2024/05/01 15:24
首先:
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
...
</servlet>
其次,在页面调用:
dwr.engine.setActiveReverseAjax(true);

用java端调用客户端:

WebContext wctx = WebContextFactory.get();
String currentPage = wctx.getCurrentPage();

// Clear the input box in the browser that kicked off this page only
Util utilThis = new Util(wctx.getScriptSession());
utilThis.setValue("text", "");

// For all the browsers on the current page:
Collection sessions = wctx.getScriptSessionsByPage(currentPage);
Util utilAll = new Util(sessions);

// Clear the list and add in the new set of messages
utilAll.removeAllOptions("chatlog");
utilAll.addOptions("chatlog", messages, "text");
注重客户端的Java调用:
WebContext wctx = WebContextFactory.get();
String currentPage = wctx.getCurrentPage();

ScriptBuffer script = new ScriptBuffer();
script.appendScript("receiveMessages(")
.appendData(messages)
.appendScript(");");

// Loop over all the users on the current page
Collection pages = wctx.getScriptSessionsByPage(currentPage);
for (Iterator it = pages.iterator(); it.hasNext();)
{
ScriptSession otherSession = (ScriptSession) it.next();
otherSession.addScript(script);
}

原创粉丝点击