什么是DWR?它有哪些功能? [中]
DWR(Direct Web Remoting)是一个WEB远程调用框架.
可以在客户端利用JavaScript直接调用服务端的Java方法并返回值给JavaScript
DWR根据Java类来动态生成JavaScrip代码.
支持Dom Trees,支持Spring,支持commons-logging
dwr.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd"> <dwr> <allow> <create creator="new" javascript="MsgPush"> <param name="class" value="com.middleware.dwr.MsgReverseAjax"/> </create> </allow> </dwr>
方法里调用
public static void pusMessage(TE_MESSAGE_ALERT message){ String org_coad = message.getRECEIVE_ORG(); String af = "pushMessage"; String type = "null"; Map<String,String> map = new HashMap<String,String>(); Message m = null; map.put("id",message.getMESSAGE_ID()+""); map.put("event_id", message.getEVENT_ID()); map.put("type", message.getEVENT_TYPE()); map.put("title", message.getEVENT_TITLE()); map.put("org_coad", org_coad); map.put("if_handle", message.getIF_HANDLE()); map.put("handle_id", message.getHANDLE_ID()); m = new Message(org_coad,type,af,map); MsgReverseAjax mra = new MsgReverseAjax(); mra.update(m); }
MsgReverseAjax.java 执行
package com.middleware.dwr; import java.util.Date; import java.util.List; import org.directwebremoting.ScriptBuffer; import org.directwebremoting.ScriptSession; public class MsgReverseAjax { private UScriptSessionCache ussc = UScriptSessionCache.getInstance(); public MsgReverseAjax() { } public void update(Message m) { List<ScriptSession> list = ussc.all(); if (list != null && list.size() > 0) { System.out.println("time : " + new Date().getTime() + " , push list "+list.size()); for (ScriptSession ss : list) { String msg = m.getMesage(); ss.addScript(new ScriptBuffer() .appendScript(m.getAjaxFunction()) .appendScript("(").appendData(msg) .appendScript(");")); } }else{ System.out.println("no scriptsession in cache"); } } }
//推送信息
function pushMessage(message) { var json_message = eval('(' + message + ')'); //判断是否为当前机构的 var org_coad = json_message.org_coad; if(org_coad == get_login_detail().subc_id){ //当前是否有未读通知 var count = $("#message_alert_count").html(); var tr_html="<tbody><tr id='"+json_message.id+"' event_id='"+json_message.event_id+"' type='"+json_message.type+"'><td style='text-align: left;cursor:pointer'>"+json_message.title+"</td></tr></tbody>"; if(count == '0'){//无数据 var html = "<table class='table_style' id='message_table' style='width=\"%100\"'>"; html+=tr_html; html+="</table>"; $("#message_alert_info").html(html); $("#message_alert_count").html('1'); }else{//存在数据 //获取所有通知数据 var all_html = $("#message_table").html(); all_html = tr_html + all_html; $("#message_table").html(all_html); //通知数量+1 $("#message_alert_count").html(Number(count)+1); } //如果当前机构是中心 if(org_coad == '0001'){ }else{//修改审阅状态 if(json_message.if_handle == '1'){//调度信息 $("#view_button_"+json_message.handle_id).html("(已审阅)"); }else{//非调度信息 if(json_message.type == '2'){//交通肇事 $(".traffic_accident_view_"+json_message.event_id).html("(已审阅)"); }else if(type == '3'){//交通阻塞 $(".view_block_id_"+json_message.event_id).html("(已审阅)"); } } } } }