- maven 依赖
<!--dwr-->
<dependency>
<groupId>org.directwebremoting</groupId>
<artifactId>dwr</artifactId>
<version>3.0.1-RELEASE</version>
</dependency>
- dwr servlet
<!-- Dwr 推技术 -->
<!-- Start -->
<servlet>
<servlet-name>dwr-invoke</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>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>initApplicationScopeCreatorsAtStartup</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoke</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<!-- End -->
- 配置dwr.xml (放在和web.xml 同一个目录)
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
"http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<!--定义了DWR能够创建和转换的类,以供 javascript 访问。-->
<allow>
<!-- <create> 标签中指定 javascript 中可以访问的 java 类,并定义 dwr 应当如何获得要进行远程的类的实例。
其中 javascript=" testClass " 属性指定 javascript 代码访问对象时使用的名称。
creator是类构造器,creator="new" 属性指定 java 类实例的生成方式, new 意味着 DWR 应当调用类的默认构造函数来获得实例,其他的还有 spring 方式,通过与 IOC 容器 Spring 进行集成来获得实例等等。 -->
<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<ScriptSession> 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(); }
-
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/13834.html