DWR写AJAX例子


昨天刚用prototype做了个AJAX的例子,今天用了下DWR
才发现AJAX竟然也可以如此傻瓜式的实现,安装和xml的配置很多地方都有,我也不写了。:

 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 ="JDate" >  
      
< param  name ="class"  value ="java.util.Date" />  
    
</ create >  
    
< create  creator ="new"  javascript ="Demo" >  
      
< param  name ="class"  value ="com.lan.Demo" />  
      
< include  method ="setinfo" />
      
< include  method ="hello" />
      
< include  method ="getaccount" />
    
</ create >  
    
< convert  match ="com.lan.Demo"  converter ="bean" >
      
< param  name ="include"  value ="name,sex,age,job,work" ></ param >
    
</ convert >
  
</ allow >  
</ dwr >  


 

jsp页面如下:

 

<% @ page language = " java "   import = " java.util.* "  pageEncoding = " gbk " %>
< jsp:directive.page  import = " com.lan.Demo " />
<%
String path 
=  request.getContextPath();
String basePath 
=  request.getScheme() + " :// " + request.getServerName() + " : " + request.getServerPort() + path + " / " ;
%>

<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html >
  
< head >
    
< base href = " <%=basePath%> " >
    
    
< title > My JSP  ' dwr.jsp '  starting page </ title >
    
    
< meta http - equiv = " pragma "  content = " no-cache " >
    
< meta http - equiv = " cache-control "  content = " no-cache " >
    
< meta http - equiv = " expires "  content = " 0 " >     
    
< meta http - equiv = " keywords "  content = " keyword1,keyword2,keyword3 " >
    
< meta http - equiv = " description "  content = " This is my page " >
    
<!--
    
< link rel = " stylesheet "  type = " text/css "  href = " styles.css " >
    
-->

< script type = ' text/javascript '  src = ' dwr/interface/JDate.js ' ></ script >  
< script type = ' text/javascript '  src = ' dwr/interface/Demo.js ' ></ script >  
< script type = ' text/javascript '  src = ' dwr/engine.js ' ></ script >  
 
< script >
    function test()
{
    Demo.hello(
"lanzhengwu",callhello);

    }


function callhello(msg)
{
alert(msg);
}


    function callback(msg) 
{
        document.getElementById(
"linkid").innerHTML=msg;
    }


        var sign
= false ;
    
        function show(uid)
{
        
if(sign==false){
        Demo.getaccount(uid,callback);
        sign
=true;
        }
else if(sign==true){
        document.getElementById(
"linkid").innerHTML="######";
        sign
=false;
        }

        }

        
    function display()
{
        Demo.setinfo(backinfo);
    }

    function backinfo(msg) 
{
    var s
="";
    s
+=msg[1]+"   ";
    s
+="姓名:"+msg[0].name+"    ";
    s
+="性别:"+msg[0].sex+"     ";
    s
+="年龄:"+msg[0].age+"    ";
    s
+="工作于:"+msg[0].work+"   ";
    s
+="职位:"+msg[0].job+"   ";
    
        document.getElementById(
"rep").innerHTML=s;
    }

 
</ script >

  
</ head >
  
  
< body >
    This is my JSP page. 
< br >
    
< input type = " button "  value = " test "  onclick = " test() " />
    
< span onclick = " show(1) "  id = " linkid "  style = " cursor:hand " > ###### </ span >
    
< a href = " # "  onmouseover = " display() "   > 放上来显示信息 </ a >
    
< div id = " rep " ></ div >
    
< table >
    
< tr >< td ></ td ></ tr >
    
</ table >
  
</ body >
</ html >

 

Bean 代码如下:

 

package  com.lan;

import  java.util.ArrayList;

public   class  Demo  {

     String name
="";
       String sex
="";
       
int age=0;
       String job
="";
       String work
="";
    
public int getAge() {
        
return age;
    }

    
public void setAge(int age) {
        
this.age = age;
    }

    
public String getJob() {
        
return job;
    }

    
public void setJob(String job) {
        
this.job = job;
    }

    
public String getName() {
        
return name;
    }

    
public void setName(String name) {
        
this.name = name;
    }

    
public String getSex() {
        
return sex;
    }

    
public void setSex(String sex) {
        
this.sex = sex;
    }

    
public String getWork() {
        
return work;
    }

    
public void setWork(String work) {
        
this.work = work;
    }

       
public String hello(String name){
            
return "您好!"+name+",开始你的DWR学习啦!";
           }

       
public double getaccount(int id){
           
double money=0.0;
           
if(id==1){
               money
=1234.00;
           }
else{
               money
=8765.00;
           }

           
return money;
           
           }
 
       
public ArrayList<Object> setinfo(){
        ArrayList
<Object> array=new ArrayList<Object>();
        Demo demo
=new Demo();
        demo.setName(
"lanzhengwu");
        demo.setAge(
19);
        demo.setSex(
"");
        demo.setJob(
"Java程序员");
        demo.setWork(
"001f");
        array.add(demo);
        array.add(
"hello");
        
return array;
       }

      
      
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值