论坛首页 Web前端技术论坛

Dwr反转Ajax

浏览 11007 次
锁定老帖子 主题:Dwr反转Ajax
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-11-08  
DWR
1. web.xml中需要增加的配置
<servlet>
    <servlet-name>dwr-invoker</servlet-name>
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>pollAndCometEnabled</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>crossDomainSessionSecurity</param-name>
      <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
pollAndCometEnabled表示开启“推”模式,crossDomainSessionSecurity表示允许跨域
2. 与web.xml同一目录增加dwr.xml的配置文件:
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 0.4//EN" "http://www.getahead.ltd.uk/dwr/dwr.dtd"> 
<dwr>
  <allow>
    <create creator="new" javascript="AlarmRegister"  scope="session">
      <param name="class" value="com.zg.alarm.AlarmRegister" />
     <include method="register" />
    </create>
  </allow>
</dwr>
com.zg.alarm.AlarmRegister是页面中调用向服务器注册接收“alarmchange”事件的类;
<include method="register" />是该类公布出来提供给前台页面调用的方法。
3. 前台页面:
Bottom.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<script type='text/javascript' src='/common/js/util.js'></script>
<script type="text/javascript" src="/dwr/engine.js"></script>
<script type='text/javascript' src='/dwr/interface/AlarmRegister.js'> </script>
<SCRIPT>
<!--
function showAlarm(alarmType){

var alarm = $("alm");
alarm.innerHTML="<MARQUEE DIRECTION=left BEHAVIOR=SCROLL SCROLLAMOUNT=10 SCROLLDELAY=200 LOOP=1><font size=3pt>告警发生变化</font></MARQUEE>";
//getMsg();
}
//-->
</SCRIPT>
</head>
<body onload="DWREngine.setActiveReverseAjax(true);AlarmRegister.register();">
<div id="alm">
</div>
</body>
</html>


<script type='text/javascript' src='/common/js/util.js'></script>
<script type="text/javascript" src="/dwr/engine.js"></script>是需要导入的javascript文件

AlarmRegister是在dwr.xml中<create creator="new" javascript="AlarmRegister"  scope="session">公布出来的,它会由dwr框架为我们自动生成。

页面中showAlarm方法负责显示告警发生变化,当后台接收到事件时会调用这一方法。

在本页面初始化时调用DWREngine.setActiveReverseAjax(true),表示开启ajax反转功能。AlarmRegister.register()会调用到后台的com.zg.alarm.AlarmRegister类的register方法。

4. 事件注册类:
AlarmRegister.java

public class AlarmRegister {
    public AlarmRegister() {
    }

    public void register(){
        WebContext wctx = WebContextFactory.get();
        final ScriptSession scriptSession = wctx.getScriptSession();
        OmlServerManager.getInstance().registerEventProcess(new AlarmEventProcessor(scriptSession),"alarmchange");
    }
}
向OmlServerManager注册alarmchange事件,注意的是必须把scriptSession传给事件相应类AlarmEventProcessor。

5. 事件相应类:

AlarmEventProcessor.java
public class AlarmEventProcessor implements EventProcessorInterface{
    private ScriptSession scriptSession;

    public AlarmEventProcessor(ScriptSession scriptSession) {
        this.scriptSession=scriptSession;
    }


    public void processEventMsg(String string) {
        System.out.println(string);
    }

    public void processEventMsg(OmlPDU omlPDU) {
        ScriptBuffer scriptBuffer = new ScriptBuffer();

        scriptBuffer.appendScript("showAlarm(")
                .appendData("123")
                .appendScript(");");
        scriptSession.addScript(scriptBuffer);
   
    }
  }
scriptBuffer.appendScript("showAlarm(")
                .appendData("123")
                .appendScript(");");
这一代码将产生对前台页面中javascript  showAlarm方法的调用
  • dwr.rar (348.8 KB)
  • 下载次数: 1383
   发表时间:2008-07-11  
这贴很少经典,dwr推模式。
能否将代码贴全罗
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics