Skip to content

Instantly share code, notes, and snippets.

@neophob
Created April 23, 2013 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neophob/5443241 to your computer and use it in GitHub Desktop.
Save neophob/5443241 to your computer and use it in GitHub Desktop.
XHR Action
public ActionForward execute(ActionMapping mapping, ActionForm inForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
log.debug("hello ajax!");
DatatablesCriterias dc = DatatablesCriterias.getFromRequest(request);
log.debug(dc);
AssettypeManager mgr = (AssettypeManager) getBean("assettypeManager");
List<AssetType> tmp = mgr.getAssetTypeList();
List<AssetType> assetType = new ArrayList<AssetType>();
int i=0;
for (AssetType at: tmp) {
assetType.add(at);
if (i++ >= dc.getDisplaySize()) {
break;
}
}
long count = tmp.size();
Long countFiltered = (long)assetType.size();
DataSet<AssetType> dataset = new DataSet<AssetType>(assetType, count, countFiltered);
DatatablesResponse<AssetType> resp = DatatablesResponse.build(dataset, dc);
//get output
//Accept: application/json, text/javascript, */*; q=0.01\r\n
response.setContentType("application/json");
PrintWriter out = response.getWriter();
//jackson converter
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(out, resp);
out.write("\r\n");
out.flush();
/* ArrayList<Object> list = new ArrayList<Object>();
out.write('[');
JSONObject obj=new JSONObject();
obj.put("sEcho", resp.getsEcho());
obj.put("iTotalRecords", resp.getiTotalRecords()());
obj.put("iTotalDisplayRecords", assetType.size());
boolean first=true;
for (AssetType at: assetType) {
if (first) {
first=false;
} else {
out.write(',');
}
list.clear();
list.add(at.getId());
list.add(at.getName());
list.add(at.getEpcStatus());
JSONValue.writeJSONString(list, out);
}
out.write(']');
out.write('}');
/*resp.getsEcho()
StringBuffer json = new StringBuffer(4096);
JSONValue.writeJSONString(value, out)
json.append("{");
json.append("\"sEcho\": 2,");
json.append("\"iTotalRecords\": 3,");
json.append("\"iTotalDisplayRecords\": 3,");
json.append("\"aaData\": [");
json.append("[");
json.append("\"1122\",");
json.append("\"HORST\"");
json.append("],");
json.append("[");
json.append("\"1123\",");
json.append("\"HORST2\"");
json.append("],");
json.append("[");
json.append("\"1124\",");
json.append("\"HORST3\"");
json.append("]");
json.append("]");
json.append("}");
// Write the XML to response
out.println(json);*/
Writer w = new StringWriter();
mapper.writeValue(w, resp);
log.debug("JACKSSON: "+w);
Gson gson = new Gson();
log.debug("GSON: "+gson.toJson(resp));
log.debug("done");
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment