在Controller类返回的ModelMap如何在Extjs的Grid显示呢?
后台代码:
@Controller
public class SdmsRecordController { @Autowired
private SemesterDao semesterDao; @Autowired
private StudentDisciplineItemDao studentDisciplineItemDao; @RequestMapping(value = "/getSdmsRecords.htm")
public @ResponseBody
Map<String, ? extends Object> view() {
System.out.println("success");
Semester semester = semesterDao.findActive(); List<StudentDisciplineItem> studentItems = studentDisciplineItemDao
.findAllByClass(semester.getId(), new Long(1)); Map<String, Object> modelMap = new HashMap<String, Object>();
modelMap.put("totalCount", studentItems.size());
modelMap.put("sdmsRecords", studentItems);
modelMap.put("success", true);
return modelMap;
}}
前台Extjs代码:
Ext.onReady(function() {
var writer = new Ext.data.JsonWriter( {
encode : true,
writeAllFields : true
});
var store = new Ext.data.Store( {
proxy : new Ext.data.HttpProxy( {
url : "<c:url value='/getSdmsRecords.htm' />"
}),
reader : new Ext.data.JsonReader( {
totalProperty: 'total',
successProperty: 'success',
idProperty: 'id',
root: 'sdmsRecords'
}, [ {
name : 'class'
}, {
name : 'student'
}, {
name : 'item'
}, {
name : 'time'
}, {
name : 're'
}, {
name : 'operator'
} ]),
writer : writer
}); var gridBooks = new Ext.grid.GridPanel( {
store : store,
cm : new Ext.grid.ColumnModel( {
defaults : {
sortable : true,
width : 100
},
columns : [ {
header : 'class',
dataIndex : 'class'
}, {
header : 'student',
dataIndex : 'student'
}, {
header : 'item',
dataIndex : 'item'
}, {
header : 'time',
dataIndex : 'time'
}, {
header : "re",
dataIndex : 'r'
}, {
header : "operator",
dataIndex : 'operator'
} ]
}),
width : 430,
height : 270,
title : 'ExtJS Books',
renderTo : 'sdms'
}); store.load();
});
//-->
</script>
后台代码:
@Controller
public class SdmsRecordController { @Autowired
private SemesterDao semesterDao; @Autowired
private StudentDisciplineItemDao studentDisciplineItemDao; @RequestMapping(value = "/getSdmsRecords.htm")
public @ResponseBody
Map<String, ? extends Object> view() {
System.out.println("success");
Semester semester = semesterDao.findActive(); List<StudentDisciplineItem> studentItems = studentDisciplineItemDao
.findAllByClass(semester.getId(), new Long(1)); Map<String, Object> modelMap = new HashMap<String, Object>();
modelMap.put("totalCount", studentItems.size());
modelMap.put("sdmsRecords", studentItems);
modelMap.put("success", true);
return modelMap;
}}
前台Extjs代码:
Ext.onReady(function() {
var writer = new Ext.data.JsonWriter( {
encode : true,
writeAllFields : true
});
var store = new Ext.data.Store( {
proxy : new Ext.data.HttpProxy( {
url : "<c:url value='/getSdmsRecords.htm' />"
}),
reader : new Ext.data.JsonReader( {
totalProperty: 'total',
successProperty: 'success',
idProperty: 'id',
root: 'sdmsRecords'
}, [ {
name : 'class'
}, {
name : 'student'
}, {
name : 'item'
}, {
name : 'time'
}, {
name : 're'
}, {
name : 'operator'
} ]),
writer : writer
}); var gridBooks = new Ext.grid.GridPanel( {
store : store,
cm : new Ext.grid.ColumnModel( {
defaults : {
sortable : true,
width : 100
},
columns : [ {
header : 'class',
dataIndex : 'class'
}, {
header : 'student',
dataIndex : 'student'
}, {
header : 'item',
dataIndex : 'item'
}, {
header : 'time',
dataIndex : 'time'
}, {
header : "re",
dataIndex : 'r'
}, {
header : "operator",
dataIndex : 'operator'
} ]
}),
width : 430,
height : 270,
title : 'ExtJS Books',
renderTo : 'sdms'
}); store.load();
});
//-->
</script>
HttpServletResponse res) throws Exception {
String modelId = req.getParameter("modelId");
String start = req.getParameter("start");
String limit = req.getParameter("limit"); if (StringUtils.isBlank(modelId)) {
throw new IllegalArgumentException("paramId is null");
}
JSONObject json = new JSONObject();
JSONArray jsonArr = new JSONArray(); List<ModelParam> modelParamList = modelService.getModelParamByPage(
Long.valueOf(modelId), Integer.valueOf(start),
Integer.valueOf(limit));
long totalCount = modelService.getModelParamCount(Long.valueOf(modelId));
if (modelParamList != null && modelParamList.size() > 0) {
for (ModelParam param : modelParamList) {
if (param != null) {
HashMap hm = new HashMap();
hm.put("uuid", param.getUuid());
hm.put("paramCode", param.getParamCode());
hm.put("paramName", param.getParamName());
hm.put("paramValue", param.getParamValue());
hm.put("paramType", param.getParamType());
hm.put("paramExpr", param.getParamExpr());
hm.put("modelId", param.getModelId());
hm.put("namePosition", param.getNamePosition());
hm.put("valuePosition", param.getValuePosition());
jsonArr.add(hm);
}
}
}
json.put("data", jsonArr);
json.put("totalCount", totalCount);
res.setCharacterEncoding("UTF-8");
res.getWriter().print(json.toString());
return null;
}
看看这个
利用您的帮助,问题解决。
要传递的数据的是一个Object然后这个Object里面有一个属性又包含了另外一个Object,那传递json的时候怎么控制呢?
例如:
public Person{
private Strng name;
private Addresss address;//address是一个自定义Object
//getter和setter部分省略
}
public Address{
private Strng rode;
private String street;
//getter和setter部分省略
}
在利用你上面的方法传递json过去的时候,在前台怎么获取呢?谢谢。