dwr小例子最简单的

前端之家收集整理的这篇文章主要介绍了dwr小例子最简单的前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

web.xml文件的编写

  <servlet>
      <servlet-name>dwr-invoker</servlet-name> 
 <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
  <init-param> 
    <param-name>debug</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>



添加dwr.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
 <dwr> 
   <allow> 
     <create creator="new" javascript="Demo"> 
         <param name="class" value="com.Demo"/> 
     </create> 
   </allow> 
</dwr> 



java类文件

package com;

public class Demo {
   public String sayHello(String name) { 
      return "Hello," + name; 
   } 
}



html文件demo.xtml

  <title>Text Demo</title> 
  <script src='dwr/interface/Demo.js'></script> <!--此处的Demo.js是dwr.xml中create标签的javascript的值-->
  <script src='dwr/engine.js'></script> 
  <script type='text/javascript' src='dwr/util.js'></script>
  <script> 
  
     var allname;
     function update() 
     { 
        
       var name = dwr.util.getValue("demoName");
 <!--在这里直接类名.方法就可以调用java方法了--> 
 Demo.sayHello(name,function(data){ 
             dwr.util.setValue("demoReply",data); 
             allname=data;
         }); 
        alert(allname);
     }
    </script> 
    </head> 
    <body> 
    <p> Name: <input type="text" id="demoName"/> 
              <input value="Send" type="button" onclick="update()"/>
               <br/> 
        Reply: <span id="demoReply"></span> 
    </p> 
    </body> 
    </html>


还要导入Dwr所需的jar文件,可以从dwr的官网下载

web.xml文件和dwr.xml文件需要放在同一级目录下

原文链接:https://www.f2er.com/ajax/165264.html

猜你在找的Ajax相关文章