前端js代码:
autoinit(0,4) functionrefresh(pageindex){ varparent=document.getElementById("contentlist"); while(parent.childNodes.length>2){ parent.removeChild(parent.lastChild); } autoinit(pageindex,4) } functionautoinit(pageindex,pagesize){ $.ajax({ type:"post",url:"<%=request.getContextPath()%>/servletpage",data:{"pageindex":pageindex,"pagesize":pagesize},success:function(returndata){ varreturndata=eval(returndata); for(vari=0;i<pagesize;i++){ newtr=document.createElement("tr"); newtdtitle=document.createElement("td"); newtdtitle.innerHTML=returndata[i].title; newtdtime=document.createElement("td"); newtdtime.innerHTML=""; newtduser=document.createElement("td"); newtduser.innerHTML=returndata[i].user; newtr.appendChild(newtdtitle); newtr.appendChild(newtdtime); newtr.appendChild(newtduser); document.getElementById("contentlist").appendChild(newtr) } } });
后台处理:
intpageindex=Integer.parseInt(request.getParameter("pageindex")); intpagesize=Integer.parseInt(request.getParameter("pagesize")); try{ List<Info>infos=newInfoDao().getInfos(pageindex,pagesize); JSONArrayja=newJSONArray(); for(inti=0;i<infos.size();i++){ JSONObjectjo=newJSONObject(); jo.put("title",infos.get(i).getTitle()); jo.put("user",infos.get(i).getPubuser()); ja.add(jo); } PrintWriterout=response.getWriter(); out.print(ja); }catch(ClassNotFoundException|sqlExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }原文链接:https://www.f2er.com/ajax/162222.html