`
泡咖啡
  • 浏览: 9271 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

jquery ajax 取多行数据库记录,在jsp页面遍历。使用Gson。

    博客分类:
  • web
阅读更多
1、action代码

@SuppressWarnings("unchecked")
	public String getChargeMode(){
		//根据产品id      获取计费方式    
		String productid ="402881833492ec97013492f512870003";
		List<ChargeMode>  vmchargeList =  chargeModeService.findByProductId(productid);
		
		//New个 Gson对象
		Gson gson = new Gson();
		//直接toJson 真是Gson的强大之处
		String result = gson.toJson(vmchargeList);
		
		System.out.println(result);
	//基类定义的 返回形式	
        return ajaxText(result);
		 
	}


2、hibernate实体
        private String id;
        //transient这个关键字是过滤不必要的底层信息
	transient
	private Product product;	 
	private String chargeName;
	private String timeType;
	private String unit;


3、jsp页面

jQuery.ajax({
dataType: "json",
type: "post",
url: "<%=basePath%>product!getChargeMode.action",
						 
success: function(data) {							jQuery.each(data, function(i, field){	
//遍历所有记录,   alert所有的id
	  alert(field['id']);
	   });
							 
	}
						 
});



4、action输出
//能以此格式 存储多行记录 

[{"id":"40288036379bd5b101379bd6455e0001","chargeName":"包月","timeType":"M","unit":"元/月"}
2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics