1.处理ajax的异步请求
public class JsonAction extends ActionSupport implements ServletRequestAware{ private static final long serialVersionUID = 1L; private HttpServletRequest request; private String result; //用于返回json 的数据结果,必须有getter and setter方法 public void setServletRequest(HttpServletRequest arg0) { this.request = arg0; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } /** * 处理ajax请求 * @return success */ public String excuteAjax(){ try { //获取数据,单个获取数据 String word= request.getParameter("word"); int wordcount= Integer.parseInt(request.getParameter("wordcount")); //将数据存储在map里,再转换成json类型数据,也可以自己手动构造json类型数据 Mapmap = new HashMap (); map.put("word", word); map.put("wordcount",wordcount); JSONObject json = JSONObject.fromObject(map);//将map对象转换成json类型数据 result = json.toString();//给result赋值,传递给页面 //手动构造json数据类型 result = {\'word\':\'"+word+"\'};{\'wordcount\':\'"+wordcount+"\'}" //查询结果集list数据 RowSet rs = twImpl.queryAllBook(dbId); List
2.struts.xml的配置文件
result
3.ajax.js文件
$(function(){$.ajax({ cache:false, async:false, type:"post", dataType:"json", url:'JsonAction_excuteAjax.action?timestamp='+new Date().getTime().toString(), data:{ word:word, word:wordcount //此处不能加‘,’号 }, error:function(){ alert("error occured!!!"); }, success:function(data){ var test=eval("("+data+")"); //非循环的结果获得 var word= eval("("+data+")").word; var wrodcount= eval("("+data+")").wordcount; //循环结果集 $.each(test,function(i) { desc += '