原文連接 http://www.blogjava.net/stevenjohn/archive/2012/07/07/382447.html這片文章仍是給了我很大幫助,再次表示感謝,下面我將這兩天的研究詳細記錄下來備忘,也但願能幫助到像我同樣的人。只寫過程,不寫原理(不是不寫,而是有些地方我也不太懂),下面開始:javascript
第1、在項目中引入dwr.jar,而後在web.xml中進行配置,配置以下:html
<servlet>java
<servlet-name>dwr-invoker</servlet-name>web
<servlet-class>session
org.directwebremoting.servlet.DwrServletapp
</servlet-class>函數
<init-param>spa
<param-name>crossDomainSessionSecurity</param-name>.net
<param-value>false</param-value>debug
</init-param>
<init-param>
<param-name>allowScriptTagRemoting</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>classes</param-name>
<param-value>java.lang.Object</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>
<init-param>
<param-name>maxWaitAfterWrite</param-name>
<param-value>3000</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>logLevel</param-name>
<param-value>WARN</param-value>
</init-param>
</servlet>
第二:在web.xml的同級目錄下新建dwr.xml文件,內容以下
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"
"http://getahead.org/dwr/dwr30.dtd">
<dwr>
<alow>
<create creator="new" javascript="MessagePush">
<param name="class" value="com.huatech.messageremind.service.MessagePush"/>
</create>
</alow>
</dwr>
這個是dwr的基本配置,MessagePush在頁面的javascript中使用,com.huatech.messageremind.service.MessagePush實現了想要調用的方法,MessagePush我以爲就至關於java類中的一個映射,在javascript中使用MessagePush.java類中實現的方法,便可在dwr中調用。
第三,要想使用dwr,還要在你想要推送的頁面中引入script,
<script type="text/javascript" src="<%=basepath%>dwr/engine.js"></script>
<script type="text/javascript" src="<%=basepath%>dwr/util.js"></script>
<script type="text/javascript" src="<%=basepath%>dwr/interface/MessagePush.js"></script>
能夠看見,也引入了dwr.xml中配置的javascript,engine.js和util.js是必須引入的。
以上三點都是基本配置,沒什麼好說的,想使用dwr,就得這麼作。
第四,實現消息的精準推送
消息推送簡單,可是想實現精準推送就須要作一些別的操做了
1 在任何一個用戶登陸的時候,都須要將其userId或者其餘惟一性標識放入session中,我放的是userId,
這裏就以 userId爲惟一性標識。
2 在載入想推送的頁面時,須要onload一個我在MessagePush類中實現的方法,固然了,須要使用dwr調用
js的調用方法以下:
function onPageLoad(){
var userId = '${userinfo.humanid}';
MessagePush.onPageLoad(userId);
}
<body onload="dwr.engine.setActiveReverseAjax(true);dwr.engine.setNotifyServerOnPageUnload(true);onPageLoad();> 在onload中的三個函數都是必須的,其中dwr.engine.setActiveReverseAjax(true);dwr.engine.setNotifyServerOnPageUnload(true);是dwr中的函數。
MessagePush類中實現的方法以下:
public void onPageLoad(String userId) {
ScriptSession scriptSession = WebContextFactory.get().getScriptSession();
scriptSession.setAttribute(userId, userId);
DwrScriptSessionManagerUtil dwrScriptSessionManagerUtil = new DwrScriptSessionManagerUtil();
try {
dwrScriptSessionManagerUtil.init();
} catch (ServletException e) {
e.printStackTrace();
}
}
你們注意到,onPageLoad方法中還有一個名爲DwrScriptSessionManagerUtil的類,該類以下實現:
public class DwrScriptSessionManagerUtil extends DwrServlet{
private static final long serialVersionUID = -7504612622407420071L;
public void init()
throws ServletException {
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 =((User) session.getAttribute("userinfo")).getHumanid()+"";
System.out.println("a ScriptSession is created!");
ev.getSession().setAttribute("userId", userId);
}
public void sessionDestroyed(ScriptSessionEvent ev) {
System.out.println("a ScriptSession is distroyed");
}
};
manager.addScriptSessionListener(listener);
}
}
第四步是最最重要的,爲了第四步我研究了兩天多,下面開始消息推送。
第5、消息推送
在你想要推送消息的時候,調用以下方法:
public void sendMessageAuto(String userid,String message) {
final String userId = userid ;
final String autoMessage = message;
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.appendCall("showMessage", autoMessage);
Collection<ScriptSession> sessions = Browser
.getTargetSessions();
for (ScriptSession scriptSession : sessions) {
scriptSession.addScript(script);
}
}
});
}
userid即爲你想推給消息的人,message爲你想推送的消息,你們注意到這裏script.appendCall("showMessage", autoMessage);
其中showMessage爲在想推送的頁面中的javascript方法,autoMessage是這個方法的參數,這樣那個頁面就能獲得推送的內容了,至於如何展示,就看你的須要了。
至此,一個dwr消息精準推送的步驟就寫完了,其實不少東西都不難,只是咱們不知道該怎麼用而已。