1

I am trying to display a message when my declared modelAndView object is empty that are loaded using addObject() and returned via a controller

my code somthing like...

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

i have searched some data from database and put them on this ("issuedItemList") modelAndView object as list. i want when searching statement don't found data there will display a message like "No data found"

Rana Aziz
  • 11
  • 1
  • 3

3 Answers3

2

If You are displaying message on jsp page the you can use JSTL tags fo checking the size of list, for example

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

I think this is what you are looking for...

Kuldeep Singh
  • 199
  • 1
  • 11
1

Kuldeep's answer will work if you return an empty list. If issuedItemList can be null try

<c:if test="${not empty issuedItemList}">
   <p>No data found</p>
</c:if>
Sezin Karli
  • 2,517
  • 19
  • 24
0

I would put an error message inside the ModelAndView if the list is empty:

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);
}

This will keep all logic other than some simple null-checks out of your view.

Cameron
  • 1,868
  • 3
  • 21
  • 38