PDA

View Full Version : Struts2 action pass a json to a grid



zhenyuandeng
21 Feb 2009, 7:44 PM
How can i pass a json to a js grid?

Can i use the codes in the execute method of the action?

------------------------------------------------------------
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write("{images:[{\"a\":\"1\",\"b\":\"1\"},{\"a\":\"2\",\"b\":\"2\"}]}");

tryanDLS
22 Feb 2009, 8:08 AM
Please read http://extjs.com/learn/Ext_Forum_Help

rakesh
22 Feb 2009, 9:50 AM
May be this code can help you.

public ActionForward getListOfGMRData(ActionMapping mapping,
ActionForm form, HttpServletRequest req, HttpServletResponse res)
throws Exception {

ActionForward forward = null;

List<GMRListViewDO> gmrDataList = null;

GMRListFilterDO filterDO = new GMRListFilterDO();

this.populateListingFilterDO(filterDO, req);

filterDO.setCorporateId(this.getUserContext(req).getCorporateDO()
.getCorporatePkDO().getCorporateId());

gmrDataList = LogisticBDFactory.getInstance().getLogisticBD()
.getListofGMR(filterDO);
if (!EkaCollectionUtil.isNullOrEmpty(gmrDataList)) {
this.prepareListingData(gmrDataList, res, req);
}

return forward;
}

public String prepareListingData(
List<? extends EkaListingDO> listingResults,
HttpServletResponse res, HttpServletRequest request)
throws SystemException {
String listingJSONString = null;
IUserContext userContext = this.getUserContext(request);

try {

JSONObject jsonObj = null;
JSONObject data = new JSONObject();
JSONArray jsnDataArray = new JSONArray();
String totalCount = new String();

for (EkaListingDO ekaListingDO : listingResults) {
jsonObj = new JSONObject();
jsonObj = (JSONObject) JSONSerializer.toJSON(ekaListingDO);
jsnDataArray.add(jsonObj);
totalCount = ekaListingDO.getTotalNoOfRecords();
}

data.accumulate("TotalCount", totalCount);
data.accumulate("data", jsnDataArray);

listingJSONString = data.toString();
PrintWriter out = res.getWriter();
out.println(listingJSONString);
out.flush();

} catch (JSONException e) {
logger.error(userContext, e);

} catch (Exception e) {
logger.error(userContext, e);
}
return listingJSONString;
}

var gmrDataStore = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url: '/listOfGMR.do?method=getListOfGMRData',
method: 'POST'
}),
reader: new Ext.data.JsonReader({
root: "data",
totalProperty: "TotalCount"
}, record)
});