转载自:http://blog.sina.com.cn/s/blog_a345a8960101krct.html
有几个必须要的包
ezmorph-1.0.3.jar
json-lib-2.1.jar
struts2-json-plugin-2.1.8.1.jar
commons-lang-2.4.jar
commons-beanutils-1.7.0.jar
Action部分
@Controller
public class LookAction extends ActionSupport {
private int thispage; //设置私有变量,用于接受Jsp页面Ajxa传参(get、set方法要写上)
public int getThispage() {
return thispage;
}
public void setThispage(int thispage) {
this.thispage = thispage;
}
private JSONArray jarray; //创建JSONArray 用于向页面传递Json格式数据(get、set方法要写上)
public JSONArray getJarray() {
return jarray;
}
public void setJarray(JSONArray jarray) {
this.jarray = jarray;
}
@Resource LookServiceImp lookService;
@Override
public String execute() throws Exception {
Page page = new Page(thispage, 8);//Page分页工具类
List list = (List)lookService.getPage(page); //调用ServiceImp得到查询对象集合
JSONArray json = new JSONArray().fromObject(list);//创建JSONArray 用fromObject方法装入list集合
jarray = json; //把集合对象转化为Json格式集合对象
return SUCCESS;
}
}
Struts.xml
<!-- 引用Json类型 成功后传递Action中编译好的json格式对象 -->
Jsp页面接收
$.post("look.action",
{thispage:page,},
function(data){
alert(list.id)
}
});