加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

DWR3+spring mvc实现

发布时间:2020-12-16 01:45:28 所属栏目:百科 来源:网络整理
导读:前篇介绍了dwr3的用法,需要的童学请移步到这里:http://www.jb51.cc/article/p-vqrknipz-eu.html 这里在前篇的基础上介绍下dwr3与spring mvc的结合使用 修改一下dwr.xml的配置,把 creator = "new" 换成 creator = "spring", name = "class"换成 = "beanNa

前篇介绍了dwr3的用法,需要的童学请移步到这里:http://www.52php.cn/article/p-vqrknipz-eu.html

这里在前篇的基础上介绍下dwr3与spring mvc的结合使用

修改一下dwr.xml的配置,把creator="new" 换成creator="spring",name="class"换成="beanName"

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC
    "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
    "http://www.getahead.ltd.uk/dwr/dwr30.dtd">
<dwr>
	<allow>
		<create creator="spring" javascript="MessagePush">
			<param name="beanName" value="MessagePush" />
		</create>
	</allow>
</dwr> 

在applicationContext.xml配置里面把主类加载一次,添加下面的配置即可
<bean id="MessagePush" class="com.coreware.dwr.MessagePush">  
</bean>

MessagePush做少少的改动,只要是添加一些注解 @Controller、 @RemoteProxy、 @RemoteMethod
package com.coreware.dwr;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.directwebremoting.Browser;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ScriptSessionFilter;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
@RemoteProxy
public class MessagePush {
	private String userName;

	public void onPageLoad(final String tag) {
		System.out.println("onPageLoad...");
		// 获取当前的ScriptSession
		ScriptSession scriptSession = WebContextFactory.get().getScriptSession();
		scriptSession.setAttribute("tag",tag);
		userName=tag;
		System.out.println("userName="+userName);
	}
	
	@RemoteMethod
	public void send(String content){	
		System.out.println("content="+content);
		// 过滤器
		ScriptSessionFilter filter = new ScriptSessionFilter() {
			public boolean match(ScriptSession scriptSession) {
				String tag = (String) scriptSession.getAttribute("tag");
				System.out.println("tag="+tag);
				return userName.equals(tag);
			}
		};

		Runnable run = new Runnable() {
			private ScriptBuffer script = new ScriptBuffer();
			public void run() {
				System.out.println("run....");
				// 设置要调用的 js及参数
				script.appendCall("show",content);
				// 得到所有ScriptSession
				Collection<ScriptSession> sessions = DWRScriptSessionListener.getScriptSessions();
				// 遍历每一个ScriptSession
				for (ScriptSession scriptSession : sessions) {
					scriptSession.addScript(script);
				}
			}
		};
		// 执行推送
		Browser.withAllSessionsFiltered(filter,run); //注意这里调用了有filter功能的方法
	}
}
这样即可实现

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读