一Ajax回显server端的执行信息今天有人问页面可以用ajax通过事件调java方法,但有是我想把java方法执行过程中的某些提示信息自由自动的反馈给页面。该怎么办,我下来就做了一个demo.现在贴上来,供后来者参考.我用的是spring环境,所有的controller都继承子Controller;RegisteController:用来模拟用户注册的步骤,它会产生一些执行过程中的信息放到一个链表里;InfoController:用来把链表中最顶端的那条信息.发送给客户.用prototype的ajax来发送请求.发送请求的同时,发送另外一个请求用来得到执行过程中的提示信息.而这个过程递归执行,直到收到finish消息.jsp文件,用来发送ajax请求和显示回显信息.注意导入的prototype的路径.1.%@pagelanguage=javaimport=java.util.*pageEncoding=utf-8%2.!DOCTYPEHTMLPUBLIC-//W3C//DTDHTML4.01Transitional//EN3.html4.head5.titleTestingpage/title6.scripttype=text/javascriptsrc=js/prototype-1.5.1.2.js/script7.scripttype=text/javascript8.functionpostAjax(){9.varmyAjax=newAjax.Request(10.'register.do',11.{12.method:'post',13.parameters:{username:'username',password:'password'},14.onComplete:doComplete15.}16.);17.alert(requestsend.);18.getInfo();19.}20.functiongetInfo(){21.varinfoAjax=newAjax.Request(22.'info.do',23.{24.method:'get',25.parameters:{},26.onComplete:doResult27.}28.);29.}30.31.functiondoResult(req){32.if(req.responseText=='finish'){33.return;34.}35.//alert(req.responseText);36.varmyDiv=document.createElement(div);37.varinfoText=document.createTextNode(req.responseText);38.myDiv.appendChild(infoText);39.$(info).appendChild(myDiv);40.//循环发送请求得到执行信息.41.getInfo();42.}43.44.functiondoComplete(){45.46.}47./script48.49.styletype=text/css50.html,body{51.font-family:Verdana,Arial,Helvetica,sans-serif;52.font-size:11pt;53.margin:0;54.padding:0;55.border:0none;56.overflow:hidden;57.height:100%;58.}59..title{60.font-weight:bold;61.}62./style63./head64.body65.divahref=javascript:postAjax();点击将提交一个ajax请求,模拟执行用户注册行为.这个请求得执行过程将显示在下面./a/div66.divid=infostyle=width:400px;height:200px;overflow:scroll;border:solidred1px;67.divclass=titleTheresponseinformationcomesfromserver:/div68.69./div70./body71./htmlRegisteController:1.importjava.util.LinkedList;2.importjavax.servlet.http.HttpServletRequest;3.importjavax.servlet.http.HttpServletResponse;4.5.importorg.apache.commons.logging.Log;6.importorg.apache.commons.logging.LogFactory;7.importorg.springframework.web.servlet.ModelAndView;8.importorg.springframework.web.servlet.mvc.Controller;9.10.publicclassRegisteControllerimplementsController{11.12.privateLoglog=LogFactory.getLog(getClass());13.14.publicModelAndViewhandleRequest(HttpServletRequestarg0,15.HttpServletResponsearg1)throwsException{16.Stringusername=arg0.getParameter(username);17.Stringpassword=arg0.getParameter(password);18.log.info(username:+username+password:+password);19./**20.*下面的infoList将用来存放执行过程中的提示信息.21.*之所以用LinkedList是因为LinkedList具有队列操做的特点.22.*最先放进去的信息可以最先被取走.23.*/24.LinkedListStringinfoList=newLinkedListString();25./**26.*这里放的信息仅仅是起到模拟作用.27.*我一次性把所有的信息都放进去了.只是为了模拟效果.28.*你可以根据实际需要,在需要的时候再放入信息.29.*/30.infoList.add(username:+username);31.//dosomethingelse.32.infoList.add(password:+password);33.//dosomethingelse.34.infoList.add(Trytovalidateusername......);35.//dosomethingelse.36.infoList.add(thisusernameisvalidtouse);37.//dosomethingelse.38.infoList.add(Trytovalidatepassword......);39.//dosomethingelse.40.infoList.add(thepasswordisvalidtouse);41.//dosomethingelse.42.infoList.add(Trytoaddthisuserintodatabase......);43.//dosomethingelse.44.infoList.add(trytosetentitlementforthisuser);45.//dosomethingelse.46.infoList.add(userregisteringok.);47.//dosomethingelse.48.infoList.add(succeed......);49.arg0.getSession().setAttribute(infoList,infoList);50.arg1.getWriter().write(succeed);51.returnnull;52.}53.54.}55.InfoController:1.importjava.util.LinkedList;2.importjavax.servlet.http.HttpServletRequest;3.importjavax.servlet.http.HttpServletResponse;4.importorg.springframework.web.servlet.ModelAndView;5.importorg.springframework.web.servlet.mvc.Controller;6./**7.*@authorAdministrator8.*这个controller用来向客户端发送执行过程中产生的信息.9.*/10.publicclassInfoControllerimplementsController{11.12.publicModelAndViewhandleRequest(HttpServletRequestarg0,13.HttpServletResponsearg1)throwsException{14.LinkedListStringlist=(LinkedListString)arg0.getSession().getAttribute(infoList);15./**16.*为了模拟延迟效果,这里睡一小会儿.17.*/18.Thread.sleep(1000);19.if(list==null){20.arg1.getWriter().write(pleasewaitasecond.);21.returnnull;22.}23.if(list.size()==0){24.arg1.getWriter().write(finish);25.returnnull;26.}27./**28.*linkedList.remove()将取走第一条信息.29.*/30.Stringinfo=list.remove();31.arg1.getWriter().write(info);32.returnnull;33.}34.35.}36.spring配置文件:1.......2.beanname=/register.doclass=com.fydy.action.RegisteController/3.beanname=/info.doclass=com.fydy.action.InfoController/4......二AJAX级联菜单AJAX级联菜单实例说明ajax级联菜单,例如,一级菜单选择中国,二级菜单列出中国城市;一级菜单选择美国,二级菜单列出美国城市。数据库脚本count.sqlDROPTABLEIFEXISTS`country`;CREATETABLE`country`(`id`int(11)NOTNULLauto_increment,`name`varchar(50)defaultNULL,`city`varchar(50)defaultNULL,PRIMARYKEY(`id`))ENGINE=MyISAMDEFAULTCHARSET=utf8;insertintocountryvalues(1,'中国','沈阳');insertintocountryvalues(2,'中国','北京');insertintocountryvalues(3,'美国','洛杉矶');insertintocountryvalues(4,'美国','纽约');DAOpackageedu.syict.