近來發(fā)現(xiàn)controller里有不同的處理返回數(shù)據(jù)的方式,其中包括ModelMap不明白是如何處理數(shù)據(jù)返回的,所以在此記錄如下:
1.說到如何處理返回數(shù)據(jù)贯卦,要涉及到j(luò)sp的九大內(nèi)置對象及其作用域
2.還要涉及到j(luò)query的ajax請求方法
ModelMap是什么
ModelMap
實現(xiàn)了Map
接口右核,包含Map
方法,addAttribute
方法,addAttributes
方法。在視圖層可以通過reauest
來找到ModelMap
的數(shù)據(jù)坪蚁。
ModelMap的使用
@RequestMapping("/qryHHGoodsVolumeByTime")
public void qryHHGoodsVolumeByTime(HttpServletRequest request, ModelMap map){
logger.debug("MultimodalTransportController -- qryHHGoodsTypeByTime");
String startDate = StringUtils.trimToEmpty(request.getParameter("startDate"));
String endDate = StringUtils.trimToEmpty(request.getParameter("endDate"));
map.addAttribute("HHVOLUME_DATA", multimodalTransportService.qryHHGoodsVolumeByTime(startDate, endDate));
map.addAttribute("MONTHDATA_LIST", multimodalTransportService.qryMonthDataListByTime(startDate, endDate));
map.addAttribute("SUCCESS", true);
}
使用分析
因為ModelMap
是存放在視圖層的request
里面的全局變量,容易造成異常镜沽,所以不建議使用這種方式敏晤。
替代方式
不再在void
方法里面將數(shù)據(jù)塞到modelMap
對象里面,而是直接新建返回一個Map
對象缅茉。
@RequestMapping("/qryHHGoodsVolumeByTime")
public Map<String,Object> qryHHGoodsVolumeByTime(HttpServletRequest request, ModelMap modelMap){
logger.debug("MultimodalTransportController -- qryHHGoodsTypeByTime");
String startDate = StringUtils.trimToEmpty(request.getParameter("startDate"));
String endDate = StringUtils.trimToEmpty(request.getParameter("endDate"));
Map<String,Object> map=new LinkedHashMap();
map.addAttribute("HHVOLUME_DATA", multimodalTransportService.qryHHGoodsVolumeByTime(startDate, endDate));
map.addAttribute("MONTHDATA_LIST", multimodalTransportService.qryMonthDataListByTime(startDate, endDate));
map.addAttribute("SUCCESS", true);
return map;
}