• 配置使用dwr完成收邮件提示


    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这个函数,这样我们就可以完成这个功能了。



  • 相关阅读:
    SURF与SIFT
    CVMAT操作
    flask
    爬虫
    mysql基础
    WINCE6.0_CHS_SDK安装失败
    com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1443 连接到主机 localhost 的 TCP/IP 连接失败。
    office 2010每次打开word都要重新配置的解决方法
    如何使用Visual Studio 2008编译C语言
    Hibernate初学、遇到的问题
  • 原文地址:https://www.cnblogs.com/qnight/p/7535116.html
Copyright © 2020-2023  润新知