AJAX实例入门
关于AJAX的定义也好,大话也好,早有人在网上发表了汗牛充栋的文字,在这里我也不想照本宣科。
只想说说我感觉到的一些优点,对于不对,大家也可以和我讨论:
首先是异步交互,用户感觉不到页面的提交,当然也不等待页面返回。这是使用了AJAX技术的页面给用户的第一感觉。
其次是响应速度快,这也是用户强烈体验。
然后是与我们开发者相关的,复杂UI的成功处理,一直以来,我们对B/S模式的UI不如C/S模式UI丰富而苦恼。现在由于AJAX大量使用JS,使得复杂的UI的设计变得更加成功。
最后,AJAX请求的返回对象为XML文件,这也是一个潮流,就是WEBSERVICE潮流一样。易于和WEBSERVICE结合起来。
好了,闲话少说,让我们转入正题吧。
我们的第一个例子是基于Servlet为后台的一个web应用。
二、基于Servlet的AJAX这是一个很常见的UI,当用户在第一个选择框里选择ZHEJIANG时,第二个选择框要出现ZHEJIANG的城市;当用户在第一个选择框里选择JIANGSU时,第二个选择框里要出现JIANGSU的城市。
首先,我们来看配置文件web.xml,在里面配置一个servlet,跟往常一样:
<web-appversion="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>SelectCityServlet</servlet-name>
<servlet-class>com.stephen.servlet.SelectCityServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SelectCityServlet</servlet-name>
<url-pattern>/servlet/SelectCityServlet</url-pattern>
</servlet-mapping>
</web-app>
然后,来看我们的JSP文件:
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<title>MyHtml.html</title>
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="thisismypage">
<!--<linkrel="stylesheet"type="text/css"href="./styles.css">-->
</head>
<scripttype="text/javascript">
functiongetResult(stateVal){
varurl="servlet/SelectCityServlet?state="+stateVal;
if(window.XMLHttpRequest){
req=newXMLHttpRequest();
}elseif(window.ActiveXObject){
req=newActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("GET",url,true);
req.onreadystatechange=complete;
req.send(null);
}
}
functioncomplete(){
if(req.readyState==4){
if(req.status==200){
varcity=req.responseXML.getElementsByTagName("city");
//alert(city.length);
varstr=newArray();
for(vari=0;i<city.length;i++){
str[i]=city[i].firstChild.data;
}
//alert(document.getElementById("city"));
buildSelect(str,document.getElementById("city"));
}
}
}
functionbuildSelect(str,sel){
sel.options.length=0;
for(vari=0;i<str.length;i++){
sel.options[sel.options.length]=newOption(str[i],str[i])
}
}
</script>
<body>
<selectname="state"onChange="getResult(this.value)">
<optionvalue="">Select</option>>
<optionvalue="zj">ZEHJIANG</option>>
<optionvalue="zs">JIANGSU</option>>
</select>
<selectid="city">
<optionvalue="">CITY</option>
</select>
</body>
</html>
第一眼看来,跟我们平常的JSP没有两样。仔细一看,不同在JS里头。
我们首先来看第一个方法:getResult(stateVal),在这个方法里,首先是取得XmlHttpRequest;然后设置该请求的url:req.open("GET",url,true);接着设置请求返回值的接收方法:req.onreadystatechange=complete;该返回值的接收方法为——complete();最后是发送请求:req.send(null);
然后我们来看我们的返回值接收方法:complete(),这这个方法里,首先判断是否正确返回,如果正确返回,用DOM对返回的XML文件进行解析。关于DOM的使用,这里不再讲述,请大家参阅相关文档。得到city的值以后,再通过buildSelect(str,sel)方法赋值到相应的选择框里头去。
最后我们来看看Servlet文件:
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
/**
*@authorAdministrator
*
*TODOTochangethetemplateforthisgeneratedtypecommentgoto
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/
publicclassSelectCityServletextendsHttpServlet{
publicSelectCityServlet(){
super();
}
publicvoiddestroy(){
super.destroy();
}
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setContentType("text/xml");
response.setHeader("Cache-Control","no-cache");
Stringstate=request.getParameter("state");
StringBuffersb=newStringBuffer("<state>");
if("zj".equals(state)){
sb.append("<city>hangzhou</city><city>huzhou</city>");
}elseif("zs".equals(state)){
sb.append("<city>nanjing</city><city>yangzhou</city><city>suzhou</city>");
}
sb.append("</state>");
PrintWriterout=response.getWriter();
out.write(sb.toString());
out.close();
}
}
这个类也十分简单,首先是从request里取得state参数,然后根据state参数生成相应的XML文件,最后将XML文件输出到PrintWriter对象里。
到现在为止,第一个例子的代码已经全部结束。是不是比较简单?我们进入到第二个实例吧!
这次是基于JSP的AJAX的一个应用。
三、基于JSP的AJAX这种校验需要访问后台数据库,但又不希望用户在这里提交后等待,当然是AJAX技术大显身手的时候了。
首先来看显示UI的JSP:
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<title>Check.html</title>
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="thisismypage">
<!--<linkrel="stylesheet"type="text/css"href="./styles.css">-->
</head>
<scripttype="text/javascript">
varhttp_request=false;
functionsend_request(url){//初始化、指定处理函数、发送请求的函数
http_request=false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest){//Mozilla浏览器
http_request=newXMLHttpRequest();
if(http_request.overrideMimeType){//设置MiME类别
http_request.overrideMimeType('text/xml');
}
}
elseif(window.ActiveXObject){//IE浏览器
try{
http_request=newActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
http_request=newActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
if(!http_request){//异常,创建对象实例失败
window.alert("不能创建XMLHttpRequest对象实例.");
returnfalse;
}
http_request.onreadystatechange=processRequest;
//确定发送请求的方式和URL以及是否同步执行下段代码
http_request.open("GET",url,true);
http_request.send(null);
}
//处理返回信息的函数
functionprocessRequest(){
if(http_request.readyState==4){//判断对象状态
if(http_request.status==200){//信息已经成功返回,开始处理信息
alert(http_request.responseText);
}else{//页面不正常
alert("您所请求的页面有异常。");
}
}
}
functionuserCheck(){
varf=document.form1;
varusername=f.username.value;
if(username==""){
window.alert("Theusernamecannotbenull!");
f.username.focus();
returnfalse;
}
else{
send_request('check1.jsp?username='+username);
}
}
</script>
<body>
<formname="form1"action=""method="post">
UserName:<inputtype="text"name="username"value="">
<inputtype="button"name="check"value="check"onClick="userCheck()">
<inputtype="submit"name="submit"value="submit">
</form>
</body>
</html>
所有的JS都跟上一个例子一样,不同的只是对返回值的操作,这次是用alert来显示:alert(http_request.responseText);
我们来看处理逻辑JSP:
<%@pagecontentType="text/html;charset=gb2312"language="java"errorPage=""%>
<%
Stringusername=request.getParameter("username");
if("educhina".equals(username))out.print("用户名已经被注册,请更换一个用户名。");
elseout.print("用户名尚未被使用,您可以继续。");
%>
非常简单,先取得参数,然后作处理,最后将结果打印在out里。
我们的第三个例子仍然以这个唯一性校验为例子,这次结合Struts开发框架来完成AJAX的开发。
四、基于Struts的AJAX<actiontype="com.ajax.CheckAction"
scope="request"path="/ajax/check">
<forwardname="success"path="/check.jsp"/>
</action>
跟普通的Struts应用的配置一样,只是没有ActionForm的配置。
下面是Action类:
packagecom.ajax;
importjava.io.PrintWriter;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.struts.action.Action;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionForward;
importorg.apache.struts.action.ActionMapping;
importorg.apache.struts.action.DynaActionForm;
/**
*@authorAdministrator
*
*TODOTochangethetemplateforthisgeneratedtypecommentgoto
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/
publicclassCheckActionextendsAction
{
publicfinalActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,
HttpServletResponseresponse)
throwsException
{
System.out.println("haha...............................");
Stringusername=request.getParameter("username");
System.out.println(username);
Stringretn;
if("educhina".equals(username))retn="Can'tusethesamenamewiththeolduse,plsselectadifference...";
elseretn="congraducation!youcanusethisname....";
PrintWriterout=response.getWriter();
out.write(retn);
out.close();
returnmapping.findForward("success");
}
publicstaticvoidmain(String[]args)
{
}
}
我们可以看到里面的逻辑跟上例中Servlet里的逻辑一样。最后,我们来看看JSP:
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<title>Check.html</title>
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="thisismypage">
<!--<linkrel="stylesheet"type="text/css"href="./styles.css">-->
</head>
<scripttype="text/javascript">
varhttp_request=false;
functionsend_request(url){//初始化、指定处理函数、发送请求的函数
http_request=false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest){//Mozilla浏览器
http_request=newXMLHttpRequest();
if(http_request.overrideMimeType){//设置MiME类别
http_request.overrideMimeType('text/xml');
}
}
elseif(window.ActiveXObject){//IE浏览器
try{
http_request=newActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
http_request=newActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
if(!http_request){//异常,创建对象实例失败
window.alert("不能创建XMLHttpRequest对象实例.");
returnfalse;
}
http_request.onreadystatechange=processRequest;
//确定发送请求的方式和URL以及是否同步执行下段代码
http_request.open("GET",url,true);
http_request.send(null);
}
//处理返回信息的函数
functionprocessRequest(){
if(http_request.readyState==4){//判断对象状态
if(http_request.status==200){//信息已经成功返回,开始处理信息
alert(http_request.responseText);
}else{//页面不正常
alert("您所请求的页面有异常。");
}
}
}
functionuserCheck(){
varf=document.forms[0];
varusername=f.username.value;
if(username==""){
window.alert("Theusernamecannotbenull!");
f.username.focus();
returnfalse;
}
else{
send_request('ajax/check.do?username='+username);
}
}
</script>
<body>
<formname="form1"action=""method="post">
UserName:<inputtype="text"name="username"value="">
<inputtype="button"name="check"value="check"onClick="userCheck()">
<inputtype="submit"name="submit"value="submit">
</form>
</body>
</html>
我们可以看到,JSP基本是一样的,除了要发送的url:ajax/check.do?username='+username。
最后,我们来看一个基于Struts和AJAX的复杂一些的例子,如果不用AJAX技术,UI的代码将十分复杂。
五、一个复杂的实例这是一个比较复杂的级联:一共八个列表框,三个下拉框。从第一个列表框里选择到第二个列表框里后,第一个选择框里的选项是第二个列表框的选择;然后,在第一个选择框里选择以后,与选择值关联的一些选项出现在第三个列表框里。从第三个列表框里选择选项到第四个列表框里,同样,第二个选择框的选项也是第四个列表框的选项;如果对第二个选择框进行选择后,与选择值关联的一些选项出现在第六个列表框里,依次类推……
这个UI的逻辑就比较复杂,但使用了AJAX使得我们实现起来就简单多了,这个例子我们除了使用Action类,还要用到POJO类和Business类,然后我们扩展的话,可以通过Business类和数据库连接起来。
我们还是先看配置文件:
<actiontype="com.ajax.SelectAction"
scope="request"path="/ajax/select">
<forwardname="success"path="/select.jsp"/>
</action>
然后看看Action类:
/*
/**
*Title:BaseDictClass
*Description:hereDescriptionisthefunctionofclass,heremaybemultirows
*Copyright:Copyright(c)2004
*@companyFreebordersCo.,Ltd.
*@GoalFeng
*@Version1.0
*/
packagecom.ajax;
importjava.io.PrintWriter;
importjava.util.List;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.struts.action.Action;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionForward;
importorg.apache.struts.action.ActionMapping;
/**
*@authorAdministrator
*
*TODOTochangethetemplateforthisgeneratedtypecommentgoto
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/
publicclassSelectActionextendsAction
{
publicfinalActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,
HttpServletResponseresponse)
throwsException
{
response.setContentType("text/xml");
response.setHeader("Cache-Control","no-cache");
Stringtype=request.getParameter("type");
Stringid=request.getParameter("id");
System.out.println(id);
StringBuffersb=newStringBuffer("<select>");
sb.append("<type>"+type+"</type>");
Listlist=newSelectBusiness().getData(id);
for(inti=0;i<list.size();i++)
{
SelectFormsel=(SelectForm)list.get(i);
sb.append("<text>"+sel.getText()+"</text><value>"+sel.getValue()+"</value>");
}
sb.append("</select>");
PrintWriterout=response.getWriter();
out.write(sb.toString());
out.close();
System.out.println(sb.toString());
returnmapping.findForward("success");
}
publicstaticvoidmain(String[]args)
{
}
}
POJO类和Business类:
packagecom.ajax;
/**
*@authorAdministrator
*
*TODOTochangethetemplateforthisgeneratedtypecommentgoto
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/
publicclassSelectForm
{
privateStringtext;
privateStringvalue;
/**
*@returnReturnsthetext.
*/
publicStringgetText()
{
returntext;
}
/**
*@paramtextThetexttoset.
*/
publicvoidsetText(Stringtext)
{
this.text=text;
}
/**
*@returnReturnsthevalue.
*/
publicStringgetValue()
{
returnvalue;
}
/**
*@paramvalueThevaluetoset.
*/
publicvoidsetValue(Stringvalue)
{
this.value=value;
}
publicstaticvoidmain(String[]args)
{
}
}
packagecom.ajax;
importjava.util.ArrayList;
importjava.util.List;
/**
*@authorAdministrator
*
*TODOTochangethetemplateforthisgeneratedtypecommentgoto
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/
publicclassSelectBusiness
{
publicListgetData(Stringid)
{
ArrayListlist=newArrayList();
for(inti=1;i<6;i++)
{
SelectFormform=newSelectForm();
form.setText(id+i);
form.setValue(id+i);
list.add(form);
}
returnlist;
}
publicstaticvoidmain(String[]args)
{
}
}
最后是JSP文件:
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<HTML>
<HEAD>
<TITLE>FB</TITLE>
<LINKhref="images/magazin.css"type=text/cssrel=stylesheet>
<LINKhref="images/briefing.css"type=text/cssrel=stylesheet>
<METAhttp-equiv=Content-Typecontent="text/html;charset=utf-8">
<METAcontent="MSHTML6.00.2800.1498"name=GENERATOR>
<styletype="text/css">
<!--
.style1{
color:#000000;
font-weight:bold;
font-size:10pt;
}
.style2{font-size:10pt;color:#FF0000;}
-->
</style>
<scriptlanguage="javascript">
functionAdd()
{
if(document.all.Education2.style.display=='block')document.all.Education2.style.display='none';
elsedocument.all.Education2.style.display='block';
}
</script>
</HEAD>
<BODYleftMargin=0topMargin=0marginheight="0"marginwidth="0">
<scripttype="text/javascript"src="images/head.js"></script>
<scriptlanguage=javascript>
functiongetResult(type,id){
varurl="ajax/select.do?type="+type+"&id="+id;
if(window.XMLHttpRequest){
req=newXMLHttpRequest();
}elseif(window.ActiveXObject){
req=newActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("GET",url,true);
req.onreadystatechange=complete;
req.send(null);
}
}
functioncomplete(){
if(req.readyState==4){
if(req.status==200){
vartype=req.responseXML.getElementsByTagName("type");
varvalue=req.responseXML.getElementsByTagName("value");
varstr=newArray();
for(vari=0;i<value.length;i++){
str[i]=value[i].firstChild.data;
}
vartype=type[0].firstChild.data;
removeType1(type);
if(type=="0")
{
buildSelect(str,document.getElementById("select28"));
}
elseif(type=="1")
{
buildSelect(str,document.getElementById("select32"));
}
elseif(type=="2")
{
buildSelect(str,document.getElementById("select34"));
}
}
}
}
functionbuildSelect(str,sel){
sel.options.length=0;
for(vari=0;i<str.length;i++){
sel.options[sel.options.length]=newOption(str[i],str[i])
}
}
functionremoveType1(type)
{
varform=document.forms[0];
if(type=="0")
{
removeAll(form.select28);
removeAll(form.select29);
removeAll(form.select30);
removeAll(form.select31);
removeAll(form.select32);
removeAll(form.select33);
removeAll(form.select34);
removeAll(form.select35);
}
elseif(type=="1")
{
removeAll(form.select31);
removeAll(form.select32);
removeAll(form.select33);
removeAll(form.select34);
removeAll(form.select35);
}
elseif(type=="2")
{
removeAll(form.select34);
removeAll(form.select35);
}
elseif(type=="3")
{
}
}
functionselChange(f,h)
{
for(i=0;i<f.length;i++)
{
if(f.options[i].selected==true)
{
h.options.add(newOption(f.options[i].text,f.options[i].value));
f.options.remove(i);
i--;
}
}
}
functionremoveAll(obj)
{
for(i=0;i<obj.length;i++)
{
obj.options.remove(i);
i--;
}
}
functionaddAll(f,h)
{
for(i=0;i<f.length;i++)
{
h.options.add(newOption(f.options[i].text,f.options[i].value));
f.options.remove(i);
i--;
}
}
functionchangeSel(offer,getter,type)
{
removeType(type);
for(i=0;i<getter.length;i++)
{
getter.options.remove(i);
i--;
}
getter.options.add(newOption("",""));
for(i=0;i<offer.length;i++)
{
getter.options.add(newOption(offer.options[i].text,offer.options[i].value));
}
}
functionremoveType(type)
{
varform=document.forms[0];
if(type=="0")
{
removeAll(form.select25);
removeAll(form.select28);
removeAll(form.select29);
removeAll(form.select30);
removeAll(form.select31);
removeAll(form.select32);
removeAll(form.select33);
removeAll(form.select34);
removeAll(form.select35);
}
elseif(type=="1")
{
removeAll(form.select30);
removeAll(form.select31);
removeAll(form.select32);
removeAll(form.select33);
removeAll(form.select34);
removeAll(form.select35);
}
elseif(type=="2")
{
removeAll(form.select31);
removeAll(form.select34);
removeAll(form.select35);
}
elseif(type=="3")
{
}
}
functionremoveAll(obj)
{
for(i=0;i<obj.length;i++)
{
obj.options.remove(i);
i--;
}
}
</script>
<form>
<fontface="Arial,Helvetica,sans-serif"></font>
<TABLEcellSpacing=0cellPadding=0width="100%"border=0>
<TBODY>
<TR>
<TDalign=middlevAlign=topbgcolor="#eeeeee"><!--[Left&Contents&Right]------>
<TABLEcellSpacing=0cellPadding=0width=945align=center
background=images/bg2.gifborder=0>
<TBODY>
<TR>
<TDvAlign=top><TABLEcellSpacing=0cellPadding=0width="95%"align=centerborder=0>
<TBODY>
<TR>
<TDwidth="97%"align=right><palign="left">ResourceManagement >> New
Staff</TD>
</TR>
<TRheight=4>
<TD></TD>
</TR>
<TR>
<TDvalign="top"><tableclass=homeBodycellspacing=0cellpadding=0width="100%"
align=centerborder=0>
<tbody>
<tr>
<tdheight=25colspan="2"nowrap><tableclass=gridstyle="WIDTH:100%;"cellspacing=0cellpadding=3>
<tbody>
<trclass=gridHeader>
<tdwidth="26%"align=leftbgcolor="#eeeeee"><strong> Personal
Information</strong></td>
<tdwidth="74%"align=leftbgcolor="#eeeeee"><spanclass="gridAlternating">Last
Modified:06/06/2005</span></td>
</tr>
</tbody>
</table></td>
</tr>
<tr>
<tdvalign=middlenowrap><divalign="right"><inputstyle="width:50px"class="button"type="button"value="Save"
name="B323252"> <inputstyle="width:50px"class="button"type="button"value="Undo"name="B323262">
</div></td>
</tr>
<tr>
<tdcolspan=2valign="top"class="modifiedRowmodifiedProducts">
<tableid=DynamicTabsTablecellspacing=0cellpadding=0
border=0name="DynamicTabsTable">
<tbody>
<trid=DynamicTabsTR>
<scriptlanguage=JavaScript
src="images/Public.js"></script>
</tr>
</tbody>
</table>
<tablewidth="100%"border="0"cellpadding="0"cellspacing="3"bordercolorlight="#C0C0C0"bordercolordark="#FFFFFF">
<tr>
<tdnowrap><fieldsetalign="left"style="PADDING-RIGHT:8px;PADDING-LEFT:8px;PADDING-BOTTOM:8px;LINE-HEIGHT:30px;PADDING-TOP:8px;width:900px">
<legendclass=legend>FreebordersExperience</legend>
<tablewidth="100%"height="15"border="0"align="center"cellspacing="0"bordercolorlight="#C0C0C0"bordercolordark="#FFFFFF"id="table21">
<tr>
<tdnowrap><strong>FBVerticalExperience</strong></td>
<tdcolspan="2"><strong>FBCustomer
Experience</strong></td>
</tr>
<tr>
<tdheight="26"valign="top"nowrap> </td>
<tdcolspan="2"valign="top"nowrap><strong>
<selectname="select25"id="select25"style="WIDTH:179px"onchange="getResult('0',document.forms[0].select25.value);">
<optionvalue="b">b</option>
<optionvalue="c">c</option>
</select>
</strong></td>
</tr>
<tr>
<tdheight="26"valign="top"nowrap>
<tableborder="0"cellspacing="0"cellpadding="0"id="table34">
<tr>
<td><selectid="select26"style="WIDTH:180px;HEIGHT:120px"multiple="multiple"size="4"name="select26">
<optionvalue="a">a</option>
</select>
</td>
<tdwidth="50"align="middle"valign="middle"bordercolorlight="#C0C0C0"bordercolordark="#FFFFFF"class="tableblackborder"><divalign="center">
<tablewidth="80%"height="110"border="0"align="center"cellpadding="0"cellspacing="0"id="table35">
<tr>
<td><divalign="center">
<inputname="button2"type="button"class="button"id="button29"style="WIDTH:30px"value=">"onclick="selChange(document.forms[0].select26,document.forms[0].select27);changeSel(document.forms[0].select27,document.forms[0].select25,'0')"/>
</div></td>
</tr>
<tr>
<td><divalign="center">
<inputname="button2"type="button"class="button"id="button30"style="WIDTH:30px"value=">>"causesvalidation="False"onclick="addAll(document.forms[0].select26,document.forms[0].select27);changeSel(document.forms[0].select27,document.forms[0].select25,'0')"/>
</div></td>
</tr>
<tr>
<tdheight="5"><divalign="center"></div></td>
</tr>
<tr>
<td><divalign="center">
<inputname="button2"type="button"class="button"id="button31"style="WIDTH:30px"value="<"causesvalidation="False"onclick="selChange(document.forms[0].select27,document.forms[0].select26,document.forms[0].select25,1);changeSel(document.forms[0].select27,document.forms[0].select25,'0')"/>
</div></td>
</tr>
<tr>
<td><divalign="center">
<inputname="button2"type="button"class="button"id="button32"style="WIDTH:30px"value="<<"causesvalidation="False"onclick="addAll(document.forms[0].select27,document.forms[0].select26);changeSel(document.forms[0].select27,document.forms[0].select25,'0')"/>
</div></td>
</tr>
</table>
</div></td>
<td> </td>
<tdalign="right"><selectid="select27"
style="WIDTH:180px;HEIGHT:120px"multiple="multiple"size="4"
name="select27">
<optionvalue="b">b</option>
<optionvalue="c"selected>c</option>
</select></td>
</tr>
</table></td>
<tdcolspan="2"valign="top"nowrap>
<tableborder="0"cellspacing="0"cellpadding="0"id="table36">
<tr>
<td><selectid="select28"
style="WIDTH:180px;HEIGHT:120px"multiple="multiple"size="4"
name="select28">
<optionvalue="b1">b1</option>
</select></td>
<tdwidth="50"align="middle"valign="middle"bordercolorlight="#C0C0C0"bordercolordark="#FFFFFF"class="tableblackborder"><divalign="center">
<tablewidth="80%"height="110"border="0"align="center"cellpadding="0"cellspacing="0"id="table37">
<tr>
<td><divalign="center">
<inputtype="button"name="button2"id="button33"style="WIDTH:30px"value=">"onclick="selChange(document.forms[0].select28,document.forms[0].select29);changeSel(document.forms[0].select29,document.forms[0].select30,'1')"/>
</div></td>
</tr>
<tr>
<td><divalign="center">
<inputname="button2"type="button"class="button"id="button34"style="WIDTH:30px"value=">>"causesvalidation="False"onclick="addAll(document.forms[0].select28,document.forms[0].select29);changeSel(document.forms[0].select29,document.forms[0].select30,'1')"/>
</div></td>
</tr>
<tr>
<tdheight="5"><divalign="center"></div></td>
</tr>
<tr>
<td><divalign="center">
<inputname="button2"type="button"class="button"id="button35"style="WIDTH:30px"value="<"causesvalidation="False"onclick="selChange(document.forms[0].select29,document.forms[0].select28);changeSel(document.forms[0].select29,document.forms[0].select30,'1')"/>
</div></td>
</tr>
<tr>
<td><divalign="center">
<inputname="button2"type="button"class="button"id="button36"style="WIDTH:30px"value="<<"causesvalidation="False"onclick="addAll(document.forms[0].select29,document.forms[0].select28);changeSel(document.forms[0].select29,document.forms[0].select30,'1')"/>
</div></td>
</tr>
</table>
</div></td>
<td> </td>
<td><selectid="select29"
style="WIDTH:180px;HEIGHT:120px"multiple="multiple"size="4"
name="select29">
<optionvalue="b2">b2</option>
</select></td>
</tr>
</table></td>
</tr>
<tr>
<tdheight="10"colspan="2"nowrap></td>
<tdwidth="275"valign="top"> </td>
</tr>
<tr>
<tdnowrap><strong>ProjectExperience</strong></td>
<tdcolspan="2"nowrap><strong>SubProject
Experience</strong></td>
</tr>
<tr>
<tdheight="10"valign="top"nowrap><strong>
<selectname="select30"id="select30"style="WIDTH:179px"onchange="getResult('1',document.forms[0].select30.value);">
<optionvalue=""></option>
<optionvalue="b2">b2</option>
</select>
</strong></td>
<tdcolspan="2"valign="top"nowrap><strong>
<selectname="select31"id="select31"style="WIDTH:179px"onchange="getResult('2',document.forms[0].select31.value);">
<option></option>
<optionvalue="b13">b13</option>
<optionvalue="b14">b14</option>
</select>
</strong></td>
</tr>
<tr>
<tdheight="10"valign="top"nowrap>
<tableborder="0"cellspacing="0"cellpadding="0"id="table38">
<tr>
<td><selectid="select32"
style="WIDTH:180px;HEIGHT:120px"multiple="multiple"size="4"
name="select32">
<optionvalue="b11">b11</option>
<optionvalue="b12">b12</option>
</select></td>
<tdwidth="50"align="middle"valign="middle"bordercolorlight="#C0C0C0"bordercolordark="#FFFFFF"class="tableblackborder"><divalign="center">
<tablewidth="80%"height="110"border="0"align="center"cellpadding="0"cellspacing="0"id="table39">
<tr>
<td><divalign="center">
<inputname="button2"type="button"class="button"id="button37"style="WIDTH:30px"value=">"onclick="selChange(document.forms[0].select32,document.forms[0].select33);changeSel(document.forms[0].select33,document.forms[0].select31,'2')"/>
</div></td>
</tr>
<tr>
<td><divalign="center">
<inputname="button2"type="button"class="button"id="button38"style="WIDTH:30px"value=">>"causesvalidation="False"onclick="addAll(document.forms[0].select32,document.forms[0].select33);changeSel(document.forms[0].select33,document.forms[0].select31,'2')"/>
</div></td>
</tr>
<tr>
<tdheight="5"><divalign="center"></div></td>
</tr>
<tr>
<td><divalign="center">
<inputname="button2"type="button"class="button"id="button39"style="WIDTH:30px"value="<"causesvalidation="False"onclick="selChange(document.forms[0].select33,document.forms[0].select32);changeSel(document.forms[0].select33,document.forms[0].select31,'2')"/>
</div></td>
</tr>
<tr>
<td><divalign="center">
<inputname="button2"type="button"class="button"id="button40"style="WIDTH:30px"value="<<"causesvalidation="False"onclick="addAll(document.forms[0].select33,document.forms[0].select32);changeSel(document.forms[0].select33,document.forms[0].select31,'2')"/>
</div></td>
</tr>
</table>
</div></td>
<td> </td>
<td><selectid="select33"
style="WIDTH:180px;HEIGHT:120px"multiple="multiple"size="4"
name="select33">
<optionvalue="b13">b13</option>
<optionvalue="b14">b14</option>
</select></td>
</tr>
</table></td>
<tdcolspan="2"valign="top"nowrap>
<tableborder="0"cellspacing="0"cellpadding="0"id="table40">
<tr>
<td><selectid="select34"
style="WIDTH:180px;HEIGHT:120px"multiple="multiple"size="4"
name="select34">
<optionvalue="b111">b111</option>
<optionvalue="b112">b112</option>
<optionvalue="b113">b113</option>
</select></td>
<tdwidth="50"align="middle"valign="middle"bordercolorlight="#C0C0C0"bordercolordark="#FFFFFF"class="tableblackborder"><divalign="center">
<tablewidth="80%"height="110"border="0"align="center"cellpadding="0"cellspacing="0"id="table41">
<tr>
<td><divalign="center">
<inputname="button2"type="button"class="button"id="button41"style="WIDTH:30px"value=">"onclick="selChange(document.forms[0].select34,document.forms[0].select35);"/>
</div></td>
</tr>
<tr>
<td><divalign="center">
<inputname="button2"type="button"class="button"id="button42"style="WIDTH:30px"value=">>"causesvalidation="False"onclick="addAll(document.forms[0].select34,document.forms[0].select35)"/>
</div></td>
</tr>
<tr>
<tdheight="5"><divalign="center"></div></td>
</tr>
<tr>
<td><divalign="center">
<inputname="button2"type="button"class="button"id="button43"style="WIDTH:30px"value="<"causesvalidation="False"onclick="selChange(document.forms[0].select35,document.forms[0].select34);"/>
</div></td>
</tr>
<tr>
<td><divalign="center">
<inputname="button2"type="button"class="button"id="button44"style="WIDTH:30px"value="<<"causesvalidation="False"onclick="addAll(document.forms[0].select35,document.forms[0].select34)"/>
</div></td>
</tr>
</table>
</div></td>
<td> </td>
<td><selectid="select35"
style="WIDTH:180px;HEIGHT:120px"multiple="multiple"size="4"
name="select35">
<optionvalue="b114">b114</option>
<optionvalue="b115">b115</option>
<optionvalue="b116">b116</option>
</select></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table>
</fieldset></td>
</tr>
</table></td>
</tr>
</tbody>
</table></TD>
</TR>
</TBODY>
</TABLE>
<TABLEcellSpacing=0cellPadding=0width="95%"align=centerborder=0>
<TBODY>
<TRvalign="top">
<TDwidth="97%"align="left"valign="middle"><divalign="right"><inputstyle="width:50px"class="button"type="button"value="Save"
name="B32"> <inputstyle="width:50px"class="button"type="button"value="Undo"name="B3">
</div></TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
</TBODY>
</TABLE>
<scripttype="text/javascript"src="images/bottom.js"></script></TD></TR></TBODY></TABLE>
</BODY></form></HTML>
虽然这个JSP复杂多了,但是关于AJAX的代码还是跟前面的例子一样,在这里我也就不多说了,请大家自己看。
六、蛇足其实,AJAX有它的优点,同样也有它的缺点。它一般用在用户不想等待返回结果的时候和复杂的UI上;其他时候,我们还是可以使用Struts等常规方法。
一句话,不要为了使用AJAX而使用AJAX,而是要在适合于使用AJAX的地方使用AJAX。
相关推荐
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo