DWRプッシュ--HelloWorld

13928 ワード

  • maven依存
  •  
            <dependency>
                <groupId>org.directwebremotinggroupId>
                <artifactId>dwrartifactId>
                <version>3.0.1-RELEASEversion>
            dependency>
  • dwr servlet
  •  
        
        <servlet>
            <servlet-name>dwr-invokeservlet-name>
            <servlet-class>org.directwebremoting.servlet.DwrServletservlet-class>
            <init-param>
                <param-name>debugparam-name>
                <param-value>trueparam-value>
            init-param>
            <init-param>
                <param-name>activeReverseAjaxEnabledparam-name>
                <param-value>trueparam-value>
            init-param>
            <init-param>
                <param-name>initApplicationScopeCreatorsAtStartupparam-name>
                <param-value>trueparam-value>
            init-param>
            <load-on-startup>1load-on-startup>
        servlet>
        <servlet-mapping>
            <servlet-name>dwr-invokeservlet-name>
            <url-pattern>/dwr/*url-pattern>
        servlet-mapping>
        
  • dwr.xml(web.xmlと同じディレクトリに配置)
  • を構成する
    
    <dwr>
    
        <allow>
         
            <create creator="new" javascript="CheckUserStatus">
                <param name="class" value="com.lb.mil.service.CheckUserStatusService"/>
            create>
        allow>
    dwr>
  • CheckUserStatusService
  • の作成
    package com.lb.mil.service;
    
    import com.alibaba.fastjson.JSON;
    import com.lb.common.data.json.StatusResult;
    import com.lb.common.utils.ObjectUtils;
    import com.lb.mil.cache.EHCacheConfig;
    import com.lb.mil.cache.EHCacheUtil;
    import com.lb.mil.cache.data.UserState;
    import com.lb.mil.entity.MilInformation;
    import com.lb.mil.entity.MilInformationAttach;
    import org.directwebremoting.ScriptBuffer;
    import org.directwebremoting.ScriptSession;
    import org.directwebremoting.WebContext;
    import org.directwebremoting.WebContextFactory;
    import org.directwebremoting.proxy.dwr.Util;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.List;
    
    /**
     * Created by Administrator on 2016/7/18.
     */
    public class CheckUserStatusService
     {
         protected Logger logger = LoggerFactory.getLogger(CheckUserStatusService.class);
    
         public void check()
            {
                //        
                //    
    
                ScriptBuffer scriptBuffer = new ScriptBuffer(); //  js  
                WebContext webContext= WebContextFactory.get();
                String currentPage = webContext.getCurrentPage();
                ScriptSession myScSession = webContext.getScriptSession();
                if(ObjectUtils.isNotNull(offLineIds)||ObjectUtils.isNotNull(onlineIds))
                {
                     //     show  
                    scriptBuffer.appendScript("show(")
                            .appendData(JSON.toJSONString(statusResult))
                            .appendScript(");");
                    //             session
                    Collection sessions = webContext.getScriptSessionsByPage(currentPage);
                    //         session  
                    //ScriptSession sessions = webContext.getScriptSession();
                    Util util = new Util(sessions);
                    util.addScript(scriptBuffer);
                }
            }
    }
    
  • ページコール
  • jsファイルを参照(直接参照すればいい、パスdwr/はweb.xmlの中の値、最後のjs、CheckUserStatus.jsはdwr.xmlの中のcreate javascript属性値)
    <script type='text/javascript' src='${pageContext.request.contextPath }/dwr/engine.js'>script>
    <script type='text/javascript' src='${pageContext.request.contextPath }/dwr/util.js'>script>
    <script type='text/javascript' src='${pageContext.request.contextPath }/dwr/interface/CheckUserStatus.js'>script>
  • ページbodyに追加:
    <body  onload="dwr.engine.setActiveReverseAjax(true);dwr.engine.setNotifyServerOnPageUnload(true,true);dwr.engine.setErrorHandler(function(){});">
  • javaコードで参照されるjsメソッド
      function show(msg){
      //    
      }
  • を追加
  • javaコード
    //         
    setInterval(getUserStatus,5000);//1000 1  
    /**
     *       
     */
    function getUserStatus()
    {
        //check CheckUserStatusService     ,    
        CheckUserStatus.check();
    }
  • を呼び出す