关闭

dwr快速入门

标签: dwr
219人阅读 评论(0) 收藏 举报
分类:

Dwr快速入门

引入jar包:

Web.xml配置dwr

<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>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>

    </servlet>

 

    <servlet-mapping>

        <servlet-name>dwr-invoker</servlet-name>

        <url-pattern>/dwr/*</url-pattern>

</servlet-mapping>

 

在web.xml同级目录新建个dwr.xml

<?xml version="1.0"encoding="UTF-8"?>

<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting3.0//EN" "http://getahead.org/dwr/dwr30.dtd">

 

<dwr>

    <allow>

        <!--配置的一个演示类,javascript="Demo" 表示可以在页面中用Demo这个名称指向DwrDemo这个java类,类中的方法可以在前台调用 -->

        <!-- creater="new"表示每调用一次时,都需要new一个 -->

        <create creator="new" javascript="DwrTest">

            <param name="class"value="com.th.dwr.DwrTest"/>

        </create>

       

        <!-- java类库类 -->

        <create creator="new" javascript="MyDate">

            <param name="class"value="java.util.Date"/>

        </create>

 

        <!-- 测试阶段使用,运营阶段不要使用 -->

        <!-- convert元素用于数据类型转换,即java类和javascript之间相互转换

        <convertconverter="exception" match="java.lang.Exception" />

        <convert converter="bean"match="java.lang.StackTraceElement" />

        -->

    </allow>

</dwr>

新建DwrTest类:

 

import java.util.Collection;

import org.directwebremoting.ScriptBuffer;

import org.directwebremoting.WebContext;

import org.directwebremoting.WebContextFactory;

import org.directwebremoting.proxy.dwr.Util;

 

public class DwrTest {

 

       public voidsend(String msg){

             

              WebContextwebContext = WebContextFactory.get();

              CollectionsessionswebContext.getAllScriptSessions();

              ScriptBuffersb = newScriptBuffer();

              sb.appendScript("callback(");

              sb.appendScript(msg);

              sb.appendScript(")");

              Utilutil = newUtil(sessions);

              util.addScript(sb);

       }

}

 

Jsp

//前两个js固定不变,第三个js引入自己类同名的js

<scripttype='text/javascript'src='${pageContext.request.contextPath}/dwr/engine.js'></scrit>

<script type='text/javascript'src='${pageContext.request.contextPath}/dwr/util.js'></script>

<scriptlanguage="javascript"src="${pageContext.request.contextPath}/dwr/interface/DwrTest.js"> </script>

 

<script type='text/javascript'src='<%=request.getContextPath() %>/js/jquery-3.1.1.min.js'></script>

 

 

<script type="text/javascript">

       $(document).ready(

              function(){

                     dwr.engine.setActiveReverseAjax(true);

                    

                     $('#sign').click(function(){

                            DwrTest.send($('#msg').val());

                     })

              }     

       );

 

       functioncallback(msg){

              $('#ul').html($('#ul').html() + "<br/>" + msg);

       }

</script>

</head>

<body>

      

       <ul id="ul"style="color:red;font-size: 50px;"></ul>

       <input type="text"name="msg"id="msg"size="30"style="height50px;font-size: 35px;"> 

       <input type="button"id="sign"value="发布信息">

      

</body>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3367次
    • 积分:128
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:22篇
    • 译文:0篇
    • 评论:0条