您的位置:首页 > 数据库

ajax无刷新技术

2011-03-14 13:05 113 查看
ajax无刷新技术

author:heguikun
1首先在项目的lib目录下添加dwr.jar 包


2在web.xml 写dwr的配置,如下:
 <servlet>
  <servlet-name>dwr</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</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>


--------------------------------------
3.添加dwr.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"


"http://www.getahead.ltd.uk/dwr/dwr20.dtd">

<dwr>
  <allow>
  <create creator="new" javascript="XtszDwr"><!-- http://getahead.org/dwr/dwr20.dtd脚步引用时用的对象名 -->
     <param name="class" value="biz.impl.dwrPackge.XtszDwr"/>
      <!-- hrm.action.XtszAction类中暴露的方法 -->
      <include method="TextAdd"/>
      <include method="TextModify"/>
   </create>
   <convert match="entity.Jg" converter="bean"></convert>
  </allow>
 
</dwr>


4.写暴转换为脚本的bean

package biz.impl.dwrPackge;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


import biz.impl.chenfeiwu.ZpglBiz;
/*
 * 系统机构dwu
 */


/**
 * 作者:何桂坤 注释导入快捷键 Shift+alt+j
 * 时间:2011-2-25
 * 电子邮件:heguikun@foxmail.com
 * QQ:307839227
 * MSN:heguikun@msn.com
 */
public class XtszDwr    {
 //把信息存到请求中
 public String RequestPage(String page) throws Exception {
  WebContext ct=WebContextFactory.get();//Webcontext对象
   HttpServletRequest request=ct.getHttpServletRequest();//取得request
   request.setAttribute("requestPage", page);
  return page;
  
 } 
 //添加
 public boolean TextAdd(String clzString,String columnString,String jgJc) throws Exception {
  String sql="from  "+clzString+"  where "+columnString+"='"+jgJc+"'";//组织sql语句
   System.out.println("dwr中的sql语句:"+sql);
  WebContext ct=WebContextFactory.get();//Webcontext对象
   HttpServletRequest request=ct.getHttpServletRequest();//取得request
     int size= CheckSize(request, sql);//调用如下方法
     if (size>0) {
     System.out.println("该名已经存在!");
    System.out.println("返回:true");
    return true;//存在
   }else {
    System.out.println("该名可用!");
    System.out.println("返回:false");
    return false;//不存在
   }
  
 }
 //2修改前检查
 public boolean TextModify(String clzString,String columnString,String IDstring,String id,String


checkName) throws Exception {
    //1对象clz
  //2字段columnString
  //3该对象的id主键值IDstring
  //4排除的id
  //5检查的值checkName
  ///select * from dbo.JG where JG_JC='罗云篮霸' and JG_ID not in(1)
  
  String sql="from  "+clzString
  +"  where "
  +columnString+"='"+checkName
  +"'   and "+IDstring
  +"  not in("+id+")";
  //组织sql语句
  System.out.println("dwr中的sql语句:"+sql);
  
      WebContext ct=WebContextFactory.get();//Webcontext对象
   HttpServletRequest request=ct.getHttpServletRequest();//取得request
   int size= CheckSize(request, sql);//调用如下方法
      if (size>0) {
      System.out.println("该名已经存在!");
     System.out.println("返回:true");
     return true;//存在
    }else {
     System.out.println("该名可用!");
     System.out.println("返回:false");
     return false;//不存在
    }
  
 }
 ///////////////////////////////公用方法///////////////////////////////
 public static int CheckSize(HttpServletRequest request,String sql)throws Exception{
    ServletContext sc = request.getSession().getServletContext();
    ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
    ZpglBiz biz = (ZpglBiz) ac.getBean("zpglBizImpl");//取得spring中的bean
  List list=  biz.findQuery(sql);
     return list.size();
   }
}
///////////注意 第一个RequestPage(String page)方法这里没有应用,相当于没有用


5页面使用

<script src="dwr/interface/XtszDwr.js"></script>
 <script src="dwr/engine.js"></script>
 <script src="util.js"></script>


function AddEntity()
 { 
 js=document.getElementById("js.jsMc");
   var jsMc=js.value;
   if(!jsMc)
   {
   alert("角色名不能为空!");
   zw.focus();
   return;
   }
        //下面开始使用无刷新技术                       
 XtszDwr.TextAdd("Js","jsMc",jsMc,textExist)//这脚本的声明在dwr.xml,最后一个参数是回调函数
  
 }
//回调函数
 function textExist(Exist)
 {
  if(Exist==1){
   alert("添加角色已经存在!");
  }else{
     document.forms[0].action="<%=request.getContextPath()%>/xtsz.do?operate=DoAddJs";
         document.forms[0].submit();
         alert("添加角色成功!");
  }
 
 }


////此代码来自何桂坤项目
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息