一、DWR介绍
DWR(Direct Web Remoting)可用于实现javascript直接调用java函数和后台直接调用页面javascript代码,后者可用作服务端推送消息到Web前端。
二、环境搭建
1.dwr-3.0.0-RELEASE.jar(在maven中央仓库下载)
2.spring-3.2.2.RELEASE
3.jdk1.7
三、dwr配置
1.在resources下创建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>
<!-- 建立JS对象,将目标对象的方法转换成JS对象的方法 -->
<create javascript="dwrPush" creator="new">
<param name="class" value="com.yudu.dwr.DwrPush"></param>
</create>
<!-- 指定针对于特定对象的转换器 -->
<convert match="entity.*" converter="bean"></convert>
<convert match="java.lang.Throwable" converter="bean">
<param name="include" value="message"></param>
</convert>
</allow>
</dwr>
2.web.xml配置
<!-- 配置DWR前端控制器 -->
<servlet>
<servlet-name>dwrServlet</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<!-- 指定配置文件 -->
<init-param>
<param-name>config</param-name>
<!-- 如果有多个用","分开 -->
<param-value>
/WEB-INF/classes/dwr.xml
</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<!-- 设置使用反向Ajax技术 -->
<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>
<init-param>
<!--长连接只保持时间 -->
<param-name>maxWaitAfterWrite</param-name>
<param-value>60</param-value>
</init-param>
</servlet>
四、后端
package com.yudu.dwr;
import java.util.Collection;
import javax.servlet.http.HttpSession;
import org.directwebremoting.Browser;
import org.directwebremoting.Container;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ScriptSessionFilter;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.event.ScriptSessionEvent;
import org.directwebremoting.event.ScriptSessionListener;
import org.directwebremoting.extend.ScriptSessionManager;
import org.directwebremoting.servlet.DwrServlet;
import com.ccc.system.model.UserInfo;
public class DwrPush{
public void onPageLoad(String userId){
ScriptSession scriptSession = WebContextFactory.get().getScriptSession();
scriptSession.setAttribute(userId, userId);
DwrScriptSessionManagerUtil dssmu = new DwrScriptSessionManagerUtil();
dssmu.init();
}
public void sendMsg(String userid,String org){
final String userId = userid;
final String msg = org;
final String jsFunc = "receiveMsg";
Browser.withAllSessionsFiltered(new ScriptSessionFilter(){
public boolean match (ScriptSession session){
if(session.getAttribute("userId") == null) return false;
else {
return (session.getAttribute("userId")).equals(userId);
}
}
}, new Runnable(){
private ScriptBuffer script = new ScriptBuffer();
public void run(){
script = script.appendScript(jsFunc+"(");
script = script.appendData(msg);
script = script.appendScript(")");
Collection<ScriptSession> sessions = Browser.getTargetSessions();
for(ScriptSession scriptSession : sessions){
scriptSession.addScript(script);
}
}
});
}
class DwrScriptSessionManagerUtil extends DwrServlet{
private static final long serialVersionUID = -7504612622407420071L;
public void init(){
Container container = ServerContextFactory.get().getContainer();
ScriptSessionManager manager = container.getBean(ScriptSessionManager.class);
ScriptSessionListener listener = new ScriptSessionListener(){
public void sessionCreated(ScriptSessionEvent ev) {
HttpSession session = WebContextFactory.get().getSession();
String userId =((UserInfo) session.getAttribute("userinfo")).getUserInfoId()+"";
ev.getSession().setAttribute("userId", userId);
}
public void sessionDestroyed(ScriptSessionEvent ev) {}
};
manager.addScriptSessionListener(listener);
}
}
}
五、前端
1.查看http://ip:port/program/dwr/
2.html
<script type='text/javascript' src='/traveloa/dwr/engine.js'></script>
<script type='text/javascript' src='/traveloa/dwr/interface/dwrPush.js'></script>
<script type='text/javascript' src='/traveloa/dwr/util.js'></script>
<script>
function init(){
dwr.engine.setActiveReverseAjax(true);
dwr.engine.setNotifyServerOnPageUnload(true);
var userId='${userInfo.userInfoId}';
dwrPush.onPageLoad(userId);
}
window.onload = init;
function send(msg){
var userId='${userInfo.userInfoId}';
dwrPush.sendMsg(userId,msg);
}
function receiveMsg(msg){
alert(dwr.util.toDescriptiveString(msg));
}
</script>