ajax
jsp部分代码
$(function() { $.ajax({ type:'POST', url:'list1Action.action', success:function(data){ if(data.length>0){ var tr=''; $.each(data,function(index){ tr+='<tr><td>'+data[index].id+'</td><td>'+data[index].name+'</td><td>'+data[index].zuoze+'</td>'+ '<td>'+data[index].jianjie+'</td><td><a href="updateAction.action?book.id='+data[index].id+'">修改</a> <a href="delAction.action?book.id='+data[index].id+'">删除</a></td></tr>' }) $('#tb').append(tr) } }, error:function(){ alert("错误") } }); });
action
public String list1() {//json转换 ActionContext ac = ActionContext.getContext(); HttpServletResponse response = (HttpServletResponse) ac.get(ServletActionContext.HTTP_RESPONSE); HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST); List<Book> list=dao.search(); try { JSONArray array = new JSONArray(); if(list.size()>0) { for (int i = 0; i < list.size(); i++) { JSONObject object = new JSONObject(); object.put("name", list.get(i).getName()); object.put("id", list.get(i).getId()); object.put("zuoze", list.get(i).getZuoze()); object.put("jianjie", list.get(i).getJianjie()); array.add(object); } } System.out.println(array); response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); response.getWriter().write(array.toString()); } catch (IOException e) { e.printStackTrace(); } return null; }