package com.test;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.concurrent.locks.ReentrantLock;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;
public class DWRHelper {
private static LinkedList<Message> messages = new LinkedList<Message>();
private static ReentrantLock lock = new ReentrantLock(); //JDK5锁
public void addMessage(String text){
System.out.println("--enter--");
System.out.println(text);
try{
lock.lock();
if(text!=null && text.trim().length()>0){
messages.addFirst(new Message(text));
if(messages.size()>10){
messages.removeLast();
}
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
lock.unlock();
}
//获得DWR上下文
WebContext webContext = WebContextFactory.get();
//获取当前页面URL,比如/ext3/test_tag.jsp
String currentPage = webContext.getCurrentPage();
//当前脚本sessin
ScriptSession scriptSession = webContext.getScriptSession();
//设置页面控件的值
Util util = new Util(scriptSession);
util.setValue("text", "789"); //这里是清空页面输入框的值
//设置脚本sessin的属性值
scriptSession.setAttribute("uid", "cjm");
//获取脚本session的属性值
for(Iterator it=scriptSession.getAttributeNames();it.hasNext();){
String attrName = (String)it.next();
System.out.println(attrName + "=" + scriptSession.getAttribute(attrName));
}
//获取所有浏览当前页面的脚本session
Collection<ScriptSession> sessions = webContext.getScriptSessionsByPage(currentPage);
Util utilAll = new Util(sessions);
//执行客户端脚本
ScriptBuffer script = new ScriptBuffer();
script.appendScript("");
System.out.println(script);
for(ScriptSession session: sessions){
session.addScript(script);
}
//更新这些脚本session的一些元素
utilAll.removeAllOptions("messages");
utilAll.addOptions("messages", messages, "id", "text");
}
}
dwr 推技术 demo
4星 · 超过85%的资源 需积分: 3 138 浏览量
2011-05-05
14:05:13
上传
评论
收藏 3KB 7Z 举报
q138026310
- 粉丝: 3
- 资源: 26
最新资源
- libjpeg 编译所需的 Win32.mak vs编译libjpeg
- 自动驾驶-状态估计和定位-粒子滤波实现和源码.pdf
- 数据可视化-智慧物流服务中心大屏页面.zip
- yolov5,SSD 可能使用到的一些代码
- bbbbbbbbbbbbbbbbbb
- 安卓逆向学习笔记之Frida Stalker 还原OLLVM AES.docx
- 安卓逆向学习笔记之unicorn来trace还原OLLVM Base64.docx
- 基于jquery的自定义表格组件实现
- Nessus最新20240426离线安装插件all-2.0.tar.gz
- 最新版本私钥助记词碰撞器大富豪使用python进行制作通过接口的方式进行验证支持多币种多链多网络一分钟万次验证高出货率
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈