一、导入相关包
json-20090211.jar
jquery-1.8.0.min.js
json2.js
二、Action
import java.util.List;
<span style="color:#FF0000;">import org.json.JSONArray;</span>
public class DeviceAction extends STU_BaseAction implements IHDZAction {
public void deviceList() throws Exception{
PagingInfo xpageinfo = null;
int flag=0;
if(this.request.getMethod().equalsIgnoreCase("get")){
xpageinfo = new PagingInfo();
xpageinfo.setPageIndex(1);
xpageinfo.setPageSize(15);
}else
{
flag=1;
xpageinfo =(PagingInfo)HttpUtil.getBeanItem(request, PagingInfo.class, "xpageinfo.");
}
xpageinfo.setCountSQL("select count(*) from table_1000");
xpageinfo.setSelectSQL("select * from table_1000");
List<?> list=DbMaster.getDataList("YKT", xpageinfo);
<span style="color:#FF0000;">JSONArray json = new JSONArray();
json.put(list);</span>
System.out.println(json.toString());
request.setAttribute("list", list);
this.request.setAttribute("xdatalist", list);
this.request.setAttribute("xpageinfo", xpageinfo);
if(flag>0){
HttpUtil.writeResponseText(response, 200, json.toString());
}else{
String jsp="/stu/ykt/device/deviceList.jsp";
HttpUtil.forward(request, response, jsp);
}
}
}
三、页面实现
<script type="text/javascript">
function doAjax(index) {
var pageSize = document.getElementById("xpageinfo.PageSize").value;
$.ajax({
type : "POST",
url : location.href,
data : {
"xpageinfo.PageSize" : pageSize,
"xpageinfo.PageIndex" : index,
"xpageinfo.PageCount" : document.getElementById("xpageinfo.PageCount").value
},
success : function(data) {
//以下这几种方式 都可以将标准JSON格式的字符串转换成JSON对象
<span style="color:#FF0000;"> var json = eval('(' + data + ')');
//var json=JSON.parse(msg);
// var json=(new Function("return " + msg))();
// var json=msg.toJSONString();
// var json= $.toJSON(msg);
// var json=msg.toJSONString();
// var json=JSON.stringify(data);</span>
alert(json);
});
}
</script>