1.web.xml

 1 <servlet>
 2     <servlet-name>dwr-invoker</servlet-name>
 3     <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
 4     <init-param>
 5         <param-name>debug</param-name>
 6         <param-value>true</param-value>
 7     </init-param>
 8 </servlet>
 9 
10 <servlet-mapping>
11     <servlet-name>dwr-invoker</servlet-name>
12     <url-pattern>/dwr/*</url-pattern>
13 </servlet-mapping>

2.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>
    <!-- 建立JS对象,将目标对象的方法转换成JS对象的方法 -->
    <create javascript="helloSrv" creator="new">
        <param name="class" value="services.HelloServices"></param>
    </create>
    <!-- 从Spring中获取Java对象 -->
    <create javascript="deptSrv" creator="spring">
        <param name="beanName" value="deptServices"></param>
        <!-- 禁止执行 -->
        <exclude method="deleteDept" />
    </create>
    <create javascript="loginSrv" creator="spring">
        <param name="beanName" value="loginSrv"></param>
    </create>
    <!-- 指定针对于特定对象的转换器 -->
    <convert match="entity.*" converter="bean"></convert>
    <convert match="java.lang.Throwable" converter="bean">
        <param name="include" value="message"></param>
    </convert>
</allow>
</dwr>

3.前端js代码

<!-- 记得引入js,测试地址: http://localhost:8083/dwrweb/dwr/ -->
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/interface/helloSrv.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript">
    function hello(){
        //方法一
        //返回处理后的结果信息
        /*var fn = function(result){
         $("msg").innerHTML = result;
         }
         helloSrv.sayHello($("name").value, fn);*/

        //方法二
        helloSrv.sayHello($("name").value, function(result){
            $("msg").innerHTML=result;
        });

        //方法三
        //使用如下的好处为:不用导入如上三个js        
        //第一个参数: dwr访问路径,在web.xml中配置,如: <url-pattern>/dwr/*</url-pattern>        
        //第二个参数: dwr与java服务器通信变量,在dwr.xml中声明        
        //第三个参数: 服务器方法名
        //第四个参数: 页面请求参数,即服务器方法名得参数
        //第五个参数: 回调函数        
        //dwr.engine._execute("dwr", 'helloSrv', 'sayHello', $("name").value, fn);

    }
</script>
posted on 2017-12-06 15:53  吥茼の願望  阅读(185)  评论(0编辑  收藏  举报