這幾天作項目,須要一個消息提醒功能,決定用dwr實現,在dwr官網和網上找了不少資料,也沒實現精準推送,大多數的例子具體步驟寫的不清楚,不怎麼了解dwr的人看了也未必能看懂,反正我是沒看懂,那時就決定,若本身實現了,必定將具體步驟寫下來,但願能給那些和我同樣的人一些幫助,如有不明白的,能夠給小弟留言。我只寫步驟,不寫原理,下面開始。javascript
第1、在項目中引入dwr.jar,而後在web.xml中進行配置,配置以下:java
<servlet>web
<servlet-name>dwr-invoker</servlet-name>session
<servlet-class>app
org.directwebremoting.servlet.DwrServlet函數
</servlet-class>spa
<init-param>debug
<param-name>crossDomainSessionSecurity</param-name>orm
<param-value>false</param-value>xml
</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消息精準推送的步驟就寫完了,其實不少東西都不難,只是咱們不知道該怎麼用而已。