DWR推送--HelloWorld

  • 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);
            }
        }
}
  • 页面调用

    1. 引用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>
    2. 页面body 里添加:

      <body  onload="dwr.engine.setActiveReverseAjax(true);dwr.engine.setNotifyServerOnPageUnload(true,true);dwr.engine.setErrorHandler(function(){});">
    3. 添加在java代码中引用的js方法

        function show(msg){
        //添加逻辑
        }
    4. 调用java代码

      // 定时查看用户状态
      setInterval(getUserStatus,5000);//1000为1秒钟
      /**
       * 获取用户状态
       */
      function getUserStatus()
      {
          //check为CheckUserStatusService类中的方法,直接调用
          CheckUserStatus.check();
      }

你可能感兴趣的:(java)