Ajax+JSON 从后台传值到前台_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Ajax+JSON 从后台传值到前台

Ajax+JSON 从后台传值到前台

 2014/12/30 16:00:25  Singapore_Kun  程序员俱乐部  我要评论(0)
  • 摘要:Jquery代码:$(".info-box.announcement>ul>li>a").click(attachTitlePopupBehaviour);});functionattachTitlePopupBehaviour(){varidAr=$(this).attr('id').split("_");varannoId=idAr[1];$.getJSON("${contextPath}/announcementajx/getAnnoById.action",
  • 标签:Ajax JSON JS
Jquery 代码:

class="js" name="code">$(".info-box.announcement>ul>li>a").click(attachTitlePopupBehaviour); 

});
function attachTitlePopupBehaviour(){
	var idAr = $(this).attr('id').split("_"); 
	var annoId  =idAr[1];

	$.getJSON("${contextPath}/announcementajx/getAnnoById.action", 
			{announcementId:annoId},
			function(j){
				$("#annoTitle").text(j.title);
				$("#popupAgency").html(j.agency=='WOG'?'System':j.agency);							
				$("#popupContent").html(j.content);
				$("#popupPublishDate").text(j.publishDate);
				var docuAr = $.parseJSON(j.documents);//$.parseJSON parse JSON string to js object.
				$('#documentBody').html("");
				for(var i=0;i<docuAr.length;i++){
					var row="";
					if(i==0){
						row='<tr><td style="font-weight:bold;">Attach Documents:</td>'+
						'<td>'
						+'<a href="${contextPath}/announcementajx/downloadAnnoAttachDocument.action?id='+docuAr[i].id+'">'
						+docuAr[i].displayName+'</a></td>'+
						'</tr>';
					}else{
						row='<tr><td></td>'+
						'<td style="font-weight:bold">'
						+'<a href="${contextPath}/announcementajx/downloadAnnoAttachDocument.action?id='+docuAr[i].id+'">'
						+docuAr[i].displayName+'</a></td>'+
						'</tr>';
					}
					$('#documentBody').append(row);
				} 

    });
	    
		$('#announcementDialog').modal({
	        keyboard: false ,
	    	backdrop: true,
	    }) ;
}


controller 代码:

@RequestMapping(value="/getAnnoById", method = RequestMethod.GET)
	public @ResponseBody String getAnnoById(HttpServletRequest request,@RequestParam("announcementId") String announcementId) throws Exception{		
		AnnouncementMgrBD annoMgrBD = new AnnouncementMgrBD();
		Announcement announcement = annoMgrBD.getAnnouncementByID(Long.parseLong(announcementId));

		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.registerTypeAdapter(announcement.getClass(), new AnnouncementJSONAdaptor()).disableHtmlEscaping().create();
		String jsonValue =  gson.toJson(announcement); 
		
		logger.debug("jsonValue -> Ajax : jsonValue" + jsonValue);
		return jsonValue;
	}


AnnouncementJSONAdaptor 代码:

public class AnnouncementJSONAdaptor implements JsonSerializer<Announcement> {
	@Override
	public JsonElement serialize(Announcement announcement, Type typeOfSrc,
			JsonSerializationContext context) {
			JsonObject caseJson = new JsonObject();
			
			try {
				caseJson.addProperty("id", String.valueOf(announcement.getId()));
				caseJson.addProperty("publishDate", DateUtil.convertDateToDateString(announcement.getPublishDate()));
				AgencyMgrBD agencyMgrBD = new AgencyMgrBD();
				Agency agency = agencyMgrBD.getAgencyByID(announcement.getAgencyId().longValue());
				caseJson.addProperty("agency", agency.getName());
				caseJson.addProperty("title", announcement.getTitle());
				caseJson.addProperty("content", announcement.getContent());
				
				AnnoAttachDocumentMgrBD annoAttachDocumentMgrBD = new AnnoAttachDocumentMgrBD();
				List<AnnoAttachDocument> annoAttachDocuments = annoAttachDocumentMgrBD.getAnnoAttachDocumentsByAnnoID(announcement.getId());
				
				GsonBuilder gsonBuilder = new GsonBuilder();
				Gson gson = gsonBuilder.registerTypeAdapter(annoAttachDocuments.getClass(), new AnnoAttachDocumentArrayJSONAdaptor()).create();
				String docuJsonValue =  gson.toJson(annoAttachDocuments); 
				caseJson.addProperty("documents", docuJsonValue);
			} catch (ApplicationCoreException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		return caseJson;
	}
}


AnnoAttachDocumentArrayJSONAdaptor 代码:

public class AnnoAttachDocumentArrayJSONAdaptor implements JsonSerializer<List<AnnoAttachDocument>> {
	@Override
	public JsonElement serialize(List<AnnoAttachDocument> annoAttachDocumentList, Type typeOfSrc, JsonSerializationContext context) {
		JsonArray jsonArray = new JsonArray();
		for(AnnoAttachDocument annoAttachDocument : annoAttachDocumentList){
			JsonObject caseJson = new JsonObject();
			caseJson.addProperty("id", String.valueOf(annoAttachDocument.getId()));
			caseJson.addProperty("displayName", annoAttachDocument.getDisplayName());
			jsonArray.add(caseJson);
		} 
		return jsonArray;
	}
}
发表评论
用户名: 匿名