jquery+ajax处理后台返回的对象列表

1、定义的实体类

public class Role {

	private int id;             //递增主键
	private String identity;    //身份
	//省略get和set方法
}

2、控制器

	@ResponseBody
	@RequestMapping(value="selectAllRole",method=RequestMethod.GET)
	public List<Role> selectAllRole()
	{
		List<Role> roleList=roleService.selectAllRole();
		return roleList;
	}

3、jsp页面

<table class="table table-hover" id="roleListTable">
	<tr>
		<th>ID</th>
		<th>角色</th>
		<th>管理</th>
	</tr>		
</table>

4、js代码

<script>
$(document).ready(function(){
  
  //点击查看角色列表
  $("#roleListTab").click(function(){
	  $("#roleListTable tr:not(:first)").empty(); //清空table(除了第一行以外)
	  $.ajax({
			type: "get",
			url: "selectAllRole",
			data: "",
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			success: function(result) {
				//stringify()用于从一个对象解析出字符串
				jsonData = JSON.stringify(result);
				//JSON.parse用于从一个字符串中解析出json对象
				//利用each遍历json数组,i代表下标,item代表该对象
				jQuery.each(JSON.parse(jsonData), function(i,item){  
					var tr='<td>'+(i+1)+'</td>'+
					    '<td>'+item.identity+'</td>'+
					    '<td><a href="" class="btn btn-info">查看成员</a>'+
					    '<a href="deleteRole?id='+item.id+'" class="btn btn-danger">删除角色</a></td>';
					$("#roleListTable").append('<tr>'+tr+'</tr>');
				});
				$("#roleList").tab("show");
			}
		});
  });
});
</script>

5、运行效果截图
在这里插入图片描述

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值