Jquery 代码:
$(".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;
}
}