Home > Software engineering >  Don't know how to iterate over supplied "items" in <forEach> this is the error.
Don't know how to iterate over supplied "items" in <forEach> this is the error.

Time:03-12

when i hit my url i getting an exception it is saying Don't know how to iterate over supplied "items" in <forEach>

http://localhost:8081/Spring_hibernate/search?keyword=core

this is dao part

public Offer getSearchByName(String companyName) {
        Session session = sessionFactory.openSession();
        Query<Offer> query = session.createNamedQuery("findBy.app", Offer.class);
        query.setParameter("offerDetails", companyName);
        Offer offerResult = query.getSingleResult();
        return offerResult;
    }

this is the controller

@RequestMapping("/search")
 public String search(@RequestParam("keyword") String offerDetails , Model model) {
    Offer offer = offerDaoImpl.getSearchByName(offerDetails);
    System.out.println("from search method " offer.toString());
    model.addAttribute("searchObject", offer);
   return "search";
   }

this the jsp page

<c:forEach var="temp" items="${searchObject}">
       <tr>
        <td>${temp.id}</td>
        <td>${temp.companayName}</td>
        <td>${temp.offerDetails}</td>
        <td>${temp.price}</td>
        <td><a href="/Spring_hibernate/deleteOffer?offerId=${temp.id}">Delete</a></td>
        <td><a href="/Spring_hibernate/updateOffer?offerId=${temp.id}">Update</a></td>
       </tr>
   </c:forEach> 

CodePudding user response:

You can only iterate valid Object array such as Collection, Map, Iterator, Enumeration or String. Anything else can't be iterated by <c:forEach>.

CodePudding user response:

You want to show a list. Get your query result in a collection, set that collection in "searchObject".

If the query result is not a collection, then remove forEach iterator and simply access the fields (searchObject.price).

  • Related