Ajax应用实例

使用的是webwork架构

jsp页面:

js 代码
  1. //取得XMLHttpRequest对象         
  2.  if (window.ActiveXObject && !window.XMLHttpRequest) {         
  3.       window.XMLHttpRequest=function() {         
  4.           return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') != -1) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP');         
  5.        };         
  6.  }       
  7.       
  8. <script>       
  9.     function ajax(){        
  10.       //path是取得系统路径          
  11.       path = "/pim-wms";//应用目录           
  12.       var ro = document.getElementById("historyInfo");       
  13.       var req=new XMLHttpRequest();         
  14.       if (req) {         
  15.           req.onreadystatechange=function() {         
  16.               if (req.readyState==4 ) {//判断状态,4是已发送,200已完成       
  17.                  if(req.status==200){       
  18.                     //  alert(req.responseText);       
  19.                         ro.innerHTML = req.responseText;       
  20.                         ro.scrollTop = ro.scrollHeight;       
  21.                  }else{       
  22.                     ro.innerHTML = "数据加载错误。。。";       
  23.                  }       
  24.              }       
  25.          }        
  26.          req.open('POST', path+'/ajax.action');//webwork         
  27.          req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');         
  28.          req.send("type="+type+"&startTime="+startTime+"&endTime="+endTime+"&username="+userName+"&friend="+friend);//发送参数如果有参数req.send("username="+user_name);用request取得         
  29.       }             
  30.     }         
  31.     //setInterval("ajax()",10000);       
  32. </script>   

 

xwork.xml

<action name="check" class="com.dualwin.pim.action.ValidateAction" method="ajax"/>

action:

java 代码
  1. public void ajax()throws IOException {       
  2.         String returnValue = "";       
  3.  //action 处理块   
  4.         if(cmd != null && cmd.equals("contact")){       
  5.             CardContactDelegate cardConBo = new CardContactDelegate();       
  6.             returnValue = cardConBo.findSameGroupName(username,nameLast);       
  7.         }else if(cmd != null && cmd.equals("cal")){       
  8.             CalMainDelegate cmm = new CalMainDelegate();       
  9.             returnValue = cmm.findSameSummary(username,summary,dtStart,dtEnd);       
  10.         }       
  11. //设置response字符编码       
  12.         ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");        
  13.         PrintWriter pw = ServletActionContext.getResponse().getWriter();                
  14.         pw.print(returnValue);         
  15.         pw.close();         
  16.     }       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值