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("添加角色成功!");
}
}
////此代码来自何桂坤项目
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("添加角色成功!");
}
}
////此代码来自何桂坤项目
相关文章推荐
- Demo:充分利用 Ajax 技术 来体现页面局部刷新 效果(获取天气预报情况)
- 使用AJAX技术实现网页无闪自动局部刷新
- 利用AJAX技术实现网页无刷新进度条显示
- AJAX聊天室无刷新技术方案
- AJAX(异步刷新技术)
- asp.net实现无刷新ajax技术登录界面
- 利用AJAX技术实现网页无刷新进度条显示
- 所谓的ajax动态无刷新技术---WebForm_DoCallback
- 什么是Ajax无刷新技术?
- Ajax技术实现页面无刷新跳转
- Ajax(asp.net)技术实现异步刷新和控件功能示例流程
- C# .NET AJAX无刷新技术
- [原创]超级经典聊天室系统实现 Ajax+PHP+Javascrcript+Json无刷新技术倾力打造(一)
- AJAX无刷新验证技术
- 利用ajax技术实现网页无刷新进度条显示
- ajax无刷新绘图技术总结
- .NET Ajax的无刷新技术实例详解(重点)
- 在js中利用ajax技术实现不刷新更新内容
- AJAX技术(无刷新技术)在PHP开发中的应用
- Ajax+Js+Dom+Json无刷新分页技术