servlet返回json到html页面,servlet对象通过json返回到前台页面并展示

1、实体类

importjava.util.ArrayList;

publicclassObjectType {

privateString type;

privateArrayList subObjects;

publicString getType() {

returntype;

}

publicvoidsetType(String type) {

this.type = type;

}

publicArrayList getSubObjects() {

returnsubObjects;

}

publicvoidsetSubObjects(ArrayList subObjects) {

this.subObjects = subObjects;

}

}

import java.util.ArrayList;

public class ObjectType {

private String type;

private ArrayList subObjects;

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public ArrayList getSubObjects() {

return subObjects;

}

public void setSubObjects(ArrayList subObjects) {

this.subObjects = subObjects;

}

}

publicclassSubObjectType {

privateString subtype;

privatedoublefileCount;

privatedoublebytes;

privateString timeRange;

publicString getSubtype() {

returnsubtype;

}

publicvoidsetSubtype(String subtype) {

this.subtype = subtype;

}

publicdoublegetFileCount() {

returnfileCount;

}

publicvoidsetFileCount(doublefileCount) {

this.fileCount = fileCount;

}

publicdoublegetBytes() {

returnbytes;

}

publicvoidsetBytes(doublebytes) {

this.bytes = bytes;

}

publicString getTimeRange() {

returntimeRange;

}

publicvoidsetTimeRange(String timeRange) {

this.timeRange = timeRange;

}

}

public class SubObjectType {

private String subtype;

private double fileCount;

private double bytes;

private String timeRange;

public String getSubtype() {

return subtype;

}

public void setSubtype(String subtype) {

this.subtype = subtype;

}

public double getFileCount() {

return fileCount;

}

public void setFileCount(double fileCount) {

this.fileCount = fileCount;

}

public double getBytes() {

return bytes;

}

public void setBytes(double bytes) {

this.bytes = bytes;

}

public String getTimeRange() {

return timeRange;

}

public void setTimeRange(String timeRange) {

this.timeRange = timeRange;

}

}

2、servlet:得到一个对象列表ArrayList,将其转化为jsonArray

AccountInfoDao dao =newAccountInfoDao();

ArrayList objectTypes =newArrayList();

objectTypes = dao.load();

JSONObject jsonObject =newJSONObject();

jsonObject.put("categorys", objectTypes);

JSONArray jsonArray =newJSONArray();

jsonArray.add(jsonObject);

System.out.println(jsonArray);

PrintWriter out = response.getWriter();

out.write(jsonArray.toString());

AccountInfoDao dao = new AccountInfoDao();

ArrayList objectTypes = new ArrayList();

objectTypes = dao.load();

JSONObject jsonObject = new JSONObject();

jsonObject.put("categorys", objectTypes);

JSONArray jsonArray = new JSONArray();

jsonArray.add(jsonObject);

System.out.println(jsonArray);

PrintWriter out = response.getWriter();

out.write(jsonArray.toString());

3、js处理:

functionload(){

$.ajax({

type:"post",//请求方式

url:"servlet/AccountInfo",//发送请求地址

dataType:"json",

data:{//发送给数据库的数据

},

//请求成功后的回调函数有两个参数

success:function(data,textStatus){

varobjs=eval(data);//解析json对象

varobj = objs[0];

vartable = $("#table");

table.empty();

table.append('

类别文件个数文件大小时间范围');

if(obj==null|| obj==""){

table.append('

暂无数据!')

//$("#page").hide();

returnfalse;

}

varcategorys = obj.categorys;

for(vari=0;i

vartype = categorys[i].type;

varsubObjects = categorys[i].subObjects;

varlen = subObjects.length;

table.append('

'+type+''+''+subObjects[0].subtype+''+''+subObjects[0].fileCount+''+''+subObjects[0].bytes+''+''+subObjects[0].timeRange+'')

//table.append('

'+subObjects[0].subtype+''+''+subObjects[0].fileCount+''+''+subObjects[0].bytes+''+''+subObjects[0].timeRange+'');

for(varj=1;j

table.append('

'+subObjects[j].subtype+''+''+subObjects[j].fileCount+''+''+subObjects[j].bytes+''+''+subObjects[j].timeRange+'');

}

}

//为鼠标移动添加样式,鼠标所在行变色,鼠标离开行恢复原状

$("tr:even").addClass("even");

$("th").first().css("text-align","left");

$("th").first().css("padding-left","5px");

$("tr").mouseenter(function(){

$(this).addClass('bs');

});

$("tr").mouseleave(function(){

$(this).removeClass('bs');

});

}

});

}

function load(){

$.ajax({

type:"post",//请求方式

url:"servlet/AccountInfo",//发送请求地址

dataType:"json",

data:{//发送给数据库的数据

},

//请求成功后的回调函数有两个参数

success:function(data,textStatus){

var objs=eval(data); //解析json对象

var obj = objs[0];

var table = $("#table");

table.empty();

table.append('

类别文件个数文件大小时间范围');

if(obj==null || obj==""){

table.append('

暂无数据!')

//$("#page").hide();

return false;

}

var categorys = obj.categorys;

for(var i=0;i

var type = categorys[i].type;

var subObjects = categorys[i].subObjects;

var len = subObjects.length;

table.append('

'+type+''+''+subObjects[0].subtype+''+''+subObjects[0].fileCount+''+''+subObjects[0].bytes+''+''+subObjects[0].timeRange+'')

//table.append('

'+subObjects[0].subtype+''+''+subObjects[0].fileCount+''+''+subObjects[0].bytes+''+''+subObjects[0].timeRange+'');

for(var j=1;j

table.append('

'+subObjects[j].subtype+''+''+subObjects[j].fileCount+''+''+subObjects[j].bytes+''+''+subObjects[j].timeRange+'');

}

}

//为鼠标移动添加样式,鼠标所在行变色,鼠标离开行恢复原状

$("tr:even").addClass("even");

$("th").first().css("text-align","left");

$("th").first().css("padding-left","5px");

$("tr").mouseenter(function(){

$(this).addClass('bs');

});

$("tr").mouseleave(function(){

$(this).removeClass('bs');

});

}

});

}

4、jsp页面

类别文件大小文件个数时间范围
类别文件大小文件个数时间范围
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值