springMVC+dwr

直接贴代码吧。这里dwr并没有配置到spring的xml里面,因为试图配置的时候,总是找不到interface下的js。后来选择直接配置,创建方式改为spring,想必也是一样的。



dwr.xml


 <!-- 加入dwr的支持 -->
    <servlet>
        <servlet-name>dwr-invoker</servlet-name>
        <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dwr.xml</param-value>
        </init-param>
        <!--测试环境下,需要开启debug模式,线上环境需要关闭-->
        <init-param>
            <param-name>debug</param-name>
            <param-value>true</param-value>
        </init-param>
        <!--允许跨域-->
        <init-param>
            <param-name>crossDomainSessionSecurity</param-name>
            <param-value>false</param-value>
        </init-param>
        <!--脚本压缩-->
        <init-param>
            <param-name>scriptCompressed</param-name>
            <param-value>false</param-value>
        </init-param>
        <!--允许脚本标签远程-->
        <!--<init-param>-->
            <!--<param-name>allowScriptTagRemoting </param-name>-->
            <!--<param-value>true</param-value>-->
        <!--</init-param>-->
        <!--commet 方式-->
        <!--<init-param>-->
            <!--<param-name>activeReverseAjaxEnabled </param-name>-->
            <!--<param-value>true</param-value>-->
        <!--</init-param>-->
        <!--polling方式:在comet方式的基础之上,再配置以下参数-->
        <!--<init-param>-->
            <!--<param-name>org.directwebremoting.extend.ServerLoadMonitor</param-name>-->
            <!--<param-value>org.directwebremoting.impl.PollingServerLoadMonitor</param-value>-->
        <!--</init-param>-->
        <!--commet and poll 方式-->
        <init-param>
            <param-name>pollAndCometEnabled</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>disconnectedTime</param-name>
            <param-value>5000</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>dwr-invoker</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.directwebremoting.servlet.EfficientShutdownServletContextListener</listener-class>
    </listener>

    <listener>
    <listener-class>org.directwebremoting.servlet.EfficientShutdownServletContextAttributeListener</listener-class>
    </listener>

/web-inf/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>
            <!--dwr3提供的一个日志审核过滤器-->
            <!--<filter class="org.directwebremoting.filter.AuditLogAjaxFilter"/>-->

            <create creator="spring" javascript="testDwr" scope="application">
                <param name="beanName" value="testDwr" />
            </create>

        </allow>

    </dwr>

ps :

<create creator="spring" javascript="testDwr" scope="application">
                <param name="beanName" value="testDwr" />
            </create>

这段代码要说一下,creator="spring"是说把该类交由spring来管理,<param name="beanName" value="testDwr" />,这里一定得是beanName,value是你注入的名称,如@service("testDwr")。


TestDwr.java


@Service("testDwr")
public class TestDwr {

    @Autowired
    private QuestionMapper qm;

    public String hello() {
        System.out.println("3333");
        sendMsg("hello tui tui");
        return "hello world!";
    }

    public String helloworld(String world) {
        Question q = new Question();
        q.setCreator("kehui");
        q.setAnswer("yjing");
        qm.insert(q);
        return "hello " + world;
    }
}

页面调用


<script type="text/javascript" src="/dwr/util.js" ></script>
<script type="text/javascript" src="/dwr/engine.js" ></script>
<script type="text/javascript" src="/dwr/interface/testDwr.js" ></script>
<script type="text/javascript">
    testDwr.hello();
    testDwr.helloworld("this is a helloworld test", function (result) {
        alert(result);
    });

</script>


至此其实一般使用就够了。但如果要推送怎么办呢?


推送的配置,在上面的web.xml中已经配置好了。


在代码中,你要推送,可以分为在dwr中推送和在dwr外推送。这个怎么理解呢?比如你调用一个dwr的方法,在这个方法执行过程中推送,这叫dwr里面推。  如果你在一个controller方法里执行推送,这叫外部。


以下讲在springMVC中,两种推送的调用方式,我提供一个推送类,可以直接copy,一般使用都没有问题的。

import org.directwebremoting.*;
import org.directwebremoting.proxy.dwr.Util;

import javax.servlet.ServletContext;
import java.util.Collection;

/**
 * Created by kehui on 2014/8/19.
 */
public class SendMessageUtil {

    private static void sendMsg(String functionName, String pageUrl, ServerContext context, Object... params) {
        Collection<ScriptSession> sessions = context.getScriptSessionsByPage(pageUrl);
        if (sessions == null || sessions.size() == 0)
            return;
        Util util = new Util(sessions);
        if (params == null) {
            util.addFunctionCall(functionName);
            return;
        }
        switch (params.length)
        {
            case 0 :
                util.addFunctionCall(functionName);
                break;
            case 1 :
                util.addFunctionCall(functionName, params[0]);
                break;
            case 2 :
                util.addFunctionCall(functionName, params[0], params[1]);
                break;
            case 3 :
                util.addFunctionCall(functionName, params[0], params[1], params[2]);
                break;
            case 4 :
                util.addFunctionCall(functionName, params[0], params[1], params[2], params[3]);
                break;
            case 5 :
                util.addFunctionCall(functionName, params[0], params[1], params[2], params[3], params[4]);
                break;
            case 6 :
                util.addFunctionCall(functionName, params[0], params[1], params[2], params[3], params[4], params[5]);
                break;
            case 7 :
            default :
                util.addFunctionCall(functionName, params[0], params[1], params[2], params[4], params[5], params[6], params[7]);
                break;
        }
    }

    public static void sendMsg(String functionName, String pageUrl, Object... params) {
        //得到上下文
        WebContext context = WebContextFactory.get();
        sendMsg(functionName, pageUrl, context, params);
    }

    public static void sendMsgOther(String functionName, String pageUrl, ServletContext context, Object... params) {
        //得到上下文
        ServerContext serverContext = ServerContextFactory.get(context);

        sendMsg(functionName, pageUrl, serverContext, params);
    }
}

如果你在外部使用,就调sendMsgOther,如果在dwr内部使用,调sendMsg。这是一个工具类,无需其它额外的配置。

if (sessions == null || sessions.size() == 0)
            return;

这句很重要,在外部使用的时候,如果没有客户端页面,是没法推的。我是用定时器发现这个问题的,每隔5秒去推一次,但我的项目刚启动,获取不到scriptsession,报了几次空指针之后,就出socket异常,目前也没有完全解决,只是做了空指针判断,会好点,期待有更好的办法解决这个问题。


* springMVC获取servletcontext,要实现ServletContextAware

* 要用到推送的话,在页面上一定要加上一句 dwr.engine.setActiveReverseAjax(true);



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值