首页 > 技术文章 > 配置使用dwr完成收邮件提示

qnight 2017-04-13 19:43 原文

DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开发人员开发包含AJAX技术的网站。它可以允许在浏览器里的代码使用运行在WEB服务器上的JAVA函数,就像它就在浏览器里一样。

其实这些配置也是网上大神们的,只是拿出来整理下,少走弯路。

首先需要导入dwr3.0.jar,

然后在web.xml中加入

<!-- dwr配置 -->
    <servlet>
 
        <servlet-name>dwr-invoker</servlet-name>
 
        <servlet-class>
 
            org.directwebremoting.servlet.DwrServlet
 
        </servlet-class>
 
       <init-param>
 
            <param-name>crossDomainSessionSecurity</param-name>
 
               <param-value>false</param-value>
 
            </init-param>
 
        <init-param>
 
          <param-name>allowScriptTagRemoting</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>
 
        <init-param>
 
            <param-name>maxWaitAfterWrite</param-name>
 
            <param-value>3000</param-value>
 
        </init-param>
 
        <init-param>
 
            <param-name>debug</param-name>
 
            <param-value>false</param-value>
 
        </init-param>
 
        <init-param>
 
            <param-name>logLevel</param-name>
 
            <param-value>WARN</param-value> 
 
        </init-param>
 
    </servlet>
    <servlet-mapping>  
        <servlet-name>dwr-invoker</servlet-name>  
        <url-pattern>/dwr/*</url-pattern>  
    </servlet-mapping>  

在webapps下新建dwr.xml(文件名不能改)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">
 
     <dwr>
 
          <allow>
 
               <create creator="new" javascript="MessagePush">
 
                 <param name="class" value="cn.qnight.common.MessagePush"/>
 
              </create>
 
          </allow>
 
     </dwr>

再新建一个MessagePush.java就完成dwr的配置,但是我们需要做收邮件提示得还需要一些改进。
新建 DwrScriptSessionManagerUtil.java
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 = ((Employee) session.getAttribute("loginuser")).getId();
 
                ev.getSession().setAttribute("userId", userId);
 
            }
 
            public void sessionDestroyed(ScriptSessionEvent ev) {
 
 
            }
 
        };
 
        manager.addScriptSessionListener(listener);
 
    }
 
}

新建MessagePush.java 初始化
public class 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();
 
        }
 
 }
}

最后我们在定义一个服务器发送消息的实体就行了
public class SendMessageAuto {
 
      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() {
 			/* 前台接收的function 名为showMessage, 消息		         	是autoMessage */
                        script.appendCall("showMessage", autoMessage);
 
                        Collection<ScriptSession> sessions = Browser
 
                        .getTargetSessions();
 
                        for (ScriptSession scriptSession : sessions) {
 
                               scriptSession.addScript(script);
 
                        }
 
                 }
 
                 
 
          });
 
   }
}

需要在接收页面上的js
<script type="text/javascript" src="${ctx }/dwr/engine.js"></script>
<script type="text/javascript" src="${ctx }/dwr/util.js"></script>
<script type="text/javascript" src="${ctx }/dwr/interface/MessagePush.js"></script>

body 需要加入
<body onload="dwr.engine.setActiveReverseAjax(true);dwr.engine.setNotifyServerOnPageUnload(true);onPageLoad();">

加入js代码
/* dwr初始化 */
function onPageLoad(){
    var userId = '${loginuser.id}';
    MessagePush.onPageLoad(userId);
 
 }
/* dwr接收消息 */ 
function showMessage(autoMessage) {
    alertMsg.info(autoMessage);
}

客户端响应的是showMessage这个函数,这样我们就可以完成这个功能了。



推荐阅读