【问题标题】:how to display a message when modelAndView object is empty当modelAndView对象为空时如何显示消息
【发布时间】:2014-07-15 05:49:18
【问题描述】:

当我声明的 modelAndView 对象为空时,我试图显示一条消息,该对象使用 addObject() 加载并通过控制器返回

我的代码类似于...

ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("issuedItemList", itemReceiveService
                        .getIssuedItemList(itemReceive));

我已经从数据库中搜索了一些数据,并将它们作为列表放在这个(“issuedItemList”)modelAndView 对象上。我希望在搜索语句未找到数据时会显示一条消息,例如“未找到数据”

【问题讨论】:

    标签: java spring message modelandview


    【解决方案1】:

    如果您在 jsp 页面上显示消息,您可以使用 JSTL 标签来检查列表的大小,例如

    <c:if test="${fn:length(issuedItemList) eq 0}">
       <p>No data found</p>
    </c:if>
    

    我想这就是你要找的……

    【讨论】:

      【解决方案2】:

      如果您返回一个空列表,Kuldeep 的答案将起作用。 如果issuedItemList可以为null试试

      <c:if test="${not empty issuedItemList}">
         <p>No data found</p>
      </c:if>
      

      【讨论】:

        【解决方案3】:

        如果列表为空,我会在 ModelAndView 中添加一条错误消息:

        ModelAndView modelAndView = new ModelAndView();
        List<IssuedItem> issuedItemList = itemReceiveService.getIssuedItemList(itemReceive);
        if (issuedItemList == null || issuedItemList.size() == 0) {
          modelAndView.addObject("errorMessage", "No data found");
        }
        else {
          modelAndView.addObject("issuedItemList", issuedItemList);
        }
        

        这将使您无法查看除一些简单的空检查以外的所有逻辑。

        【讨论】:

          猜你喜欢
          • 2015-03-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多