Good day. The second day I struggle with the problem. I can not get data from the method to the list in the controller. Controller methods:

@RequestMapping(value = "/searchForm", method = RequestMethod.GET) public String showSearchForm(@RequestParam(value = "idProvider", required = true) Integer idProvider, Model model) throws DaoException { Supply supply = new Supply(); supply.setProvider(providerDao.findById(idProvider)); model.addAttribute("idAttribute", supply); return "formOfSearch"; } @RequestMapping(value = "/search", method = RequestMethod.POST) public String search(@RequestParam(value = "idProvider", required = true) Integer idProvider, @RequestParam(value = "department", required = false) String department, @RequestParam(value = "carNumber", required = false) String carNumber, @RequestParam(value = "arrivalDate", required = false) @DateTimeFormat(iso = ISO.DATE) LocalDate startDate, @RequestParam(value = "arrivalDate", required = false) @DateTimeFormat(iso = ISO.DATE) LocalDate endDate, @ModelAttribute("idAttribute") Supply supply, Map<String, Object> map) throws DaoException { List<Supply> supplyList = supplyDao.searchByCriteria(idProvider, department, carNumber, startDate, endDate); map.put("search", supplyList); return "searchList"; } 

The searchByCriteria method to which data should be transmitted from the controller and which should return a list of data from the database:

  @Override public List<Supply> searchByCriteria(Integer idProvider, String department, String carNumber, LocalDate startDate, LocalDate endDate) throws DaoException { try { CriteriaBuilder cb = manager.getCriteriaBuilder(); CriteriaQuery<Supply> query = cb.createQuery(Supply.class); Root<Supply> root = query.from(Supply.class); List<Predicate> predicates = new ArrayList<Predicate>(); if (idProvider != null) { predicates.add(cb.equal(root.get("provider"), idProvider)); } if (department != null) { predicates.add(cb.like(root.get("department"), department)); } if (carNumber != null) { predicates.add(cb.like(root.get("carNumber"), carNumber)); } if (startDate != null) { predicates.add(cb.between(root.<LocalDate> get("arrivalDate"), startDate, endDate)); } // Predicate[] predicatesarr = predicates.toArray(new // Predicate[predicates.size()]); // query.select(root).where(predicatesarr); query.select(root).where(predicates.toArray(new Predicate[] {})); List<Supply> list = manager.createQuery(query).getResultList(); return list; } catch (Exception e) { throw new DaoException("An error has occurred in class SupplyDaoImpl, method searchByCriteria.", e); } } 

JSP formOfSearch search form page:

  <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <%@ page contentType="text/html;charset=utf-8"%> <h1>Поиск поставок</h1> <c:url var="searchUrl" value="/supplyController/search?idProvider=${idAttribute.provider.idProvider }" /> <form:form class="form-horizontal" modelAttribute="idAttribute" method="POST" action="${searchUrl}"> <table> <tr> <td><form:label path="provider.idProvider"></form:label></td> <td ><form:input path="provider.idProvider" /></td> </tr> <tr> <td><form:label path="carNumber">Номер автомобиля:</form:label></td> <td><form:input path="carNumber" /></td> </tr> <tr> <td><form:label path="department">Отдел:</form:label></td> <td><form:input path="department" /></td> </tr> <tr> <td><form:label path="arrivalDate">Дата прихода от:</form:label></td> <td><form:input type="date" path="arrivalDate" /></td> </tr> <tr> <td><form:label path="arrivalDate">Дата прихода до:</form:label></td> <td><form:input type="date" path="arrivalDate" /></td> </tr> </table> <br> <input value="Найти" input type="submit" class="btn btn-primary" /> </form:form> 

JSP searchList results page:

  <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <h1>Список поставок</h1> <table class="table"> <thead style="background: #9AC0CD"> <th>Дата поставки</th> <th>Номер автомобиля</th> <th>Фамилия водителя</th> <th>Телефон</th> <th>Отдел</th> <th>Товар</th> <th>Документ поставщика</th> <th>Документ получателя</th> <th>Кладовщик</th> <th>Диспетчер</th> </thead> <tbody> <c:forEach items="${supplyList}" var="searchList"> <tr> <td><c:out value="${searchList.arrivalDate }" /></td> <td><c:out value="${searchList.carNumber }" /></td> <td><c:out value="${searchList.driverName }" /></td> <td><c:out value="${searchList.phone }" /></td> <td><c:out value="${searchList.department }" /></td> <td><c:out value="${searchList.product }" /></td> <td><c:out value="${searchList.vendorDocument}" /></td> <td><c:out value="${searchList.documentReceiving}" /></td> <td><c:out value="${searchList.storekeeper}" /></td> <td><c:out value="${searchList.dispatcher}" /></td> </tr> </c:forEach> </tbody> </table> 

The search method works correctly (it finds data in the database and returns a list with the necessary data), but in the controller I get an empty list. Help please solve the problem, or at least point in which direction to dig.

  • The question is not quite clear, do you not get the data in the controller by calling supplyDao.searchByCriteria... , or are they obtained there, but not displayed on the jsp page? - MrFylypenko
  • I do not receive data in the controller by calling the method. List <Supply> supplyList = supplyDao.searchByCriteria (.....). supplyList is empty. - Volodymyr Todosiuk

1 answer 1

The question is incorrectly passed data to the jsp page. You have the data obtained from the database are placed in a normal Map , and then never used. This data needs to be added using Model.addAttribute... , the method should look like this:

 @RequestMapping(value = "/search", method = RequestMethod.POST) public String search(@RequestParam(value = "idProvider", required = true) Integer idProvider, @RequestParam(value = "department", required = false) String department, @RequestParam(value = "carNumber", required = false) String carNumber, @RequestParam(value = "arrivalDate", required = false) @DateTimeFormat(iso = ISO.DATE) LocalDate startDate, @RequestParam(value = "arrivalDate", required = false) @DateTimeFormat(iso = ISO.DATE) LocalDate endDate, @ModelAttribute("idAttribute") Supply supply //, Map<String, Object> map удалить , Model model //добавил ) throws DaoException { List<Supply> supplyList = supplyDao.searchByCriteria(idProvider, department, carNumber, startDate, endDate); //Добавляем в атрибут с названием "supplyList", как на jsp, а не "search" model.addAttribute("supplyList", supplyList); return "searchList"; } 
  • It worked. Thank you very much. - Volodymyr Todosiuk