dwr 反转 轮询以及后台调用前台js方法

前端之家收集整理的这篇文章主要介绍了dwr 反转 轮询以及后台调用前台js方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

web.xml

<servlet>
		<servlet-name>dwr-invoker</servlet-name>
		<servlet-class>com.wxl.app.service.MyDwrServlet</servlet-class>
		<init-param>
			<param-name>debug</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>logLevel</param-name>
			<param-value>DEBUG</param-value>
		</init-param>
		<init-param>
			<param-name>activeReverseAjaxEnabled</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>org.directwebremoting.extend.ServerLoadMonitor</param-name>
			<param-value>org.directwebremoting.impl.PollingServerLoadMonitor</param-value>
		</init-param>
		<init-param>
			<param-name>disconnectedTime</param-name>
			<param-value>1000</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>dwr-invoker</servlet-name>
		<url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>

配置轮询参数,activeReverseAjaxEnabled设置反转,PollingServerLoadMonitor是使用轮询,disconnectedTime是轮询的时间周期

dwr.xml

<?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>
	<allow>
		<convert converter="bean" match="com.wxl.app.service.Person" />
		<create creator="new" javascript="service">
			<param name="class" value="com.wxl.app.service.HelloWorldService" />
		</create>
		<create creator="new" javascript="personService">
			<param name="class" value="com.wxl.app.service.PersonService" />
		</create>
		<create creator="new" javascript="messageService">
			<param name="class" value="com.wxl.app.service.MessageService" />
		</create>
	</allow>
</dwr>

java

package com.wxl.app.service;

import org.directwebremoting.Browser;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSessions;

public class MessageService {
	public void sendMessage(String message) {
		final ScriptBuffer script = new ScriptBuffer();
		script.appendCall("show",message);
		Browser.withCurrentPage(new Runnable() {
			public void run() {
				ScriptSessions.addScript(script);
			}
		});
	}
}
Browser.withCurrentPage()方法会把当前页面的所有ScriptSession绑定到当前线程。在run方法中的ScriptSessions是之前绑定到线程的sessions。

jsp


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'reverse.jsp' starting page</title>

		<Meta http-equiv="pragma" content="no-cache">
		<Meta http-equiv="cache-control" content="no-cache">
		<Meta http-equiv="expires" content="0">
		<Meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<Meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
		<script type='text/javascript' src='/dwr/dwr/engine.js'></script>
		<script type='text/javascript'
			src='/dwr/dwr/interface/messageService.js'></script>
			<script type='text/javascript' src='js/jquery-1.4.2.min.js'></script>
		<script type="text/javascript">
	dwr.engine.setActiveReverseAjax(true);
	//这个方法用来启动该页面的ReverseAjax功能  
	dwr.engine.setActiveReverseAjax(true);
	//设置在页面关闭时,通知服务端销毁会话  
	dwr.engine.setNotifyServerOnPageUnload(true);
	//这个函数是提供给后台推送的时候 调用的  
	function show(content) {
		$("#content").text(content);
	}


function sendMessage() {
	var msg = $("#msg").val();
	messageService.sendMessage(msg);
}
</script>
	</head>

	<body>
		This is my JSP page.
		<input type="text" id="msg" />
		<input type="button" id="btn" value="按钮" onclick="sendMessage();" />
		<br />
		<textarea id="content" rows="10" cols="20"></textarea>
		<br>
	</body>
</html>
注意要调用
dwr.engine.setActiveReverseAjax(true);
以及
dwr.engine.setNotifyServerOnPageUnload(true);

关于dwr 反转的几个重要类:

WebContextFactory get()方法返回WebContext

WebContext的几个重要方法



ServerContextFactory get()方法返回ServerContext

ServerContext的几个方法

getContainer()方法返回Container

Container的几个方法

Browser的几个方法

ScriptSessions的几个方法

Util的几个方法

原文链接:https://www.f2er.com/ajax/163977.html

猜你在找的Ajax相关文章