DWR学习1

DWR学习1

从今天开始每天坚持写一些技术性的学习文章,希望能给初学者一些帮助,同时也让自己能从中得到收获。
DWR是什么这里就不做介绍了, 关于DWR入门的网站很多,在这里给大家总结一下。

http://wiki.javascud.org/display/dwrcn/Getting+Started
http://blogger.org.cn/blog/more.asp?name=lhwork&id=21045
http://www.blogjava.net/mmwy/archive/2005/12/05/22482.html
http://directwebremoting.org/dwr/documentation
----很多,
大家有兴趣可以google或者百度到。

下面从一个简单的列子开始:

开发工具是Myeclipse+tomcat
目前DWR的版本是2.0.5,可以从http://directwebremoting.org/dwr/download下载到。
将下载的dwr.jar复制到到WEB-INF/lib下,将web.xml进行如下配置:
<? xml version = " 1.0 "  encoding = " UTF-8 " ?>
< web - app version = " 2.5 "  
    xmlns
= " http://java.sun.com/xml/ns/javaee "  
    xmlns:xsi
= " http://www.w3.org/2001/XMLSchema-instance "  
    xsi:schemaLocation
= " http://java.sun.com/xml/ns/javaee 
    http: // java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
< display - name > DWR (Direct Web Remoting) </ display - name >
< description > A demo of how to call Java on the server directly from Javascript on the client </ description >

< 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 >
</ servlet >
< servlet - mapping >
  
< servlet - name > dwr - invoker </ servlet - name >
  
< url - pattern >/ dwr /* </url-pattern>
</servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
在src目录下建立cn.edu.nankai的package,然后在建立下面的类Welcome.java
package  cn.edu.nankai;

public   class  Welcome {

    
public  String SayWelcome(String name){
        
        
return   " 南开欢迎 " + name + " " ;
        
    }
    
    


}
配置dwr.xml,在WEB-INF下建立dwr.xml,代码如下:
<! DOCTYPE dwr PUBLIC
    
" -//GetAhead Limited//DTD Direct Web Remoting 2.0//EN "
    
" http://getahead.org/dwr/dwr20.dtd " >

< dwr >
  
< allow >
    
    
< create creator = " new "  javascript = " Welcome " >
      
< param name = " class "  value = " cn.edu.nankai.Welcome " />
    
</ create >
  
</ allow >
  
</ dwr >
然后建立welcome.jsp,
<% @ page language = " java "   import = " java.util.* "  pageEncoding = " UTF-8 " %>
<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html >
< head >
  
< title > 第一个DWR程序 </ title >
  
< script type = ' text/javascript '  src = ' dwr/interface/Welcome.js ' ></ script >
  
< script type = ' text/javascript '  src = ' dwr/engine.js ' ></ script >
  
< script type = ' text/javascript '  src = ' dwr/util.js ' ></ script >
  
< script type = ' text/javascript '  src = ' welcome.js ' ></ script >
</ head >
< body >
 
< input id = " name "  type = " text "   />
< input type = ' button '  value = ' 你好 '  onclick = ' SayWelcome(); '   />  
 
< div id = " result " ></ div >
 
</ body >
</ html >
在webroot下建立welcome.js,代码如下:
function SayWelcome() {
    var name = $('name').value;
    //alert(words);
    Welcome.SayWelcome(name, callback);
}
 
function callback(msg) {
   DWRUtil.setValue('result', msg);
}

好了,可以run project了。看看是不是有些收获呢


你可能感兴趣的:(DWR学习1)