2016年6月23日 星期四

反向AJAX (DWR 四)

※web.xml

<servlet>
    <servlet-name>dwr-invoker</servlet-name>
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>activeReverseAjaxEnabled</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

官網連結

※DaoImpl.java

public class DaoImpl implements Idao {
    @Override
    public void send(String msg, int c) {
        for (int i = 0; i < c; i++) {
            Browser.withAllSessions(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    DateFormat df = new SimpleDateFormat("ss");
                    ScriptSessions.addFunctionCall("xxx", msg + df.format(new Date()));
                }
            });
        }
    }
}

官網連結

※dwr.xml

<dwr>
    <allow>
        <create creator="new" javascript="dao">
            <param name="class" value="dao.impl.DaoImpl" />
        </create>
    </allow>
</dwr>


※index.jsp

<script type='text/javascript' src='/dwrTest/dwr/engine.js'></script>
<script type='text/javascript' src='/dwrTest/dwr/interface/dao.js'></script>
<script type='text/javascript' src='/dwrTest/dwr/util.js'></script>
----------
dwr.engine.setActiveReverseAjax(true);
dwr.engine.setNotifyServerOnPageUnload(true);
    
function xxx(m){
    var p = document.createElement('p');
    p.appendChild(document.createTextNode(m));
    $('sp').appendChild(p);
}
window.onload = dao.send("bruce is me ", 5);
----------
<span id="sp"></span>


沒有留言:

張貼留言