美文网首页
Spring MVC ModelMap.md

Spring MVC ModelMap.md

作者: wu_9f41 | 来源:发表于2017-06-20 11:03 被阅读0次

近来发现controller里有不同的处理返回数据的方式,其中包括ModelMap不明白是如何处理数据返回的,所以在此记录如下:
1.说到如何处理返回数据,要涉及到jsp的九大内置对象及其作用域
2.还要涉及到jquery的ajax请求方法

ModelMap是什么

ModelMap实现了Map接口,包含Map方法,addAttribute方法,addAttributes方法。在视图层可以通过reauest来找到ModelMap的数据。

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方法里面将数据塞到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;
    }

相关文章

网友评论

      本文标题:Spring MVC ModelMap.md

      本文链接:https://www.haomeiwen.com/subject/zfsoqxtx.html