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、运行效果截图