java|jsp页面ajax返回list(里面放有对象)进行循环并插入table

table: 【java|jsp页面ajax返回list(里面放有对象)进行循环并插入table】






ajax:
function searchSource(){ //获取搜索框里的值
var keyWord=$('#keyWord').val();
var typeSource = $('#typeSource option:selected').val();
//循环数据前先删除table的所有行
var tb = document.getElementById('datas');
var rowNum=tb.rows.length;
for (i=0; i{
tb.deleteRow(i);
rowNum=rowNum-1;
i=i-1;
}
if( typeSource.length!=0 ){
$.ajax({
type: 'POST',
url: "${ctxFront}/media/PCfront/searchSource",
dataType: "json",
cache:true,
async:false,
data: {sourceName: keyWord,typeSource:typeSource},
success: function (data) {
if(data.result == 'success'){ //listSource为后台传过来的数组
var source=data.listSource;
for(i in source){
var tbBody = "";
tbBody += ""+source[i].name+"";
$("#datas").append(tbBody);
}
}else{
var tbBody = "";
tbBody += "没有您想要的素材";
$("#datas").append(tbBody);
}
}
});
}

}

java后台: @ResponseBody
@RequestMapping(value = "https://www.it610.com/article/searchSource")
public void searchSource(HttpServletRequest request,HttpServletResponse response, Model model){
MemberInfo member = isLogin(request, response);
Map result = newHashMap();
Long typeSource = RequestUtil.getLong(request, "typeSource");
String sourceName = RequestUtil.getString(request, "sourceName");
System.out.println("sourceName:"+sourceName);
System.out.println("typeSource:"+typeSource);
result.put("result", "success");
result.put("message", "获取成功!");
if(member!=null && member.getId()>0){
if(typeSource==1){
MaterialVideo materialVideo=new MaterialVideo();
materialVideo.setMemberId(member.getId());
materialVideo.setName(sourceName);
List listVideo=materialVideoService.findByMemberId(materialVideo);
System.out.println("listVideo:"+listVideo.size());
if(listVideo.size()>0){
result.put("result", "success");
result.put("listSource", listVideo);
result.put("message", "获取成功!");
}else{
result.put("result", "false");
result.put("message", "没有该素材");
}
}
if(typeSource==2){
MaterialAudio materialAudio=new MaterialAudio();
materialAudio.setMemberId(member.getId());
materialAudio.setName(sourceName);
List listAudio=materialAudioService.findByMemberId(materialAudio);
System.out.println("listAudio:"+listAudio.size());
if(listAudio.size()>0){
result.put("result", "success");
result.put("listSource", listAudio);
result.put("message", "获取成功!");
}else{
result.put("result", "false");
result.put("message", "没有该素材");
}
}
}
writeJson(request, response, result);
}



    推荐阅读