<c:foreach jsp iterate over list

Olzhas picture Olzhas · Dec 27, 2012 · Viewed 36.8k times · Source

I have searched several examples, still have not get. I am passing an List of GOOD object from controller into jsp pages. trying to loop over the list object, but its showing only one element repeatedly. should I use beans? If yes, could you provide more specific example for my case.

 <c:if test="${not empty listGood}">
     <c:forEach var="ob" varStatus="status" items="${listGood}">
    <tr>
        <td><c:out value="${ob.name}"/></td>
        <td><c:out value="${ob.measure}"/></td>
        <td><c:out value="${ob.quantity}"/></td>
        <td><c:out value="${ob.price}"/></td>
    </tr>
             </c:forEach>
            </c:if>

Update Here is the controller:

@RequestMapping(value={"/supply"}, method=RequestMethod.POST)
public String consumptFormulate(Locale locale, Model model, @ModelAttribute ConsumptionForm cmd, HttpServletRequest request){
  String[] s_str =cmd.getFromDate().split("/");
  String normal_s  = s_str[2]+"-"+s_str[0]+"-"+s_str[1];
  String[] f_str = cmd.getToDate().split("/");
   String normal_f  = f_str[2]+"-"+f_str[0]+"-"+f_str[1];
     List<Good> list = service.getGoods(normal_s,normal_f,cmd.getSocGoods(),cmd.getTradeObj());
     List<ListGoodsForm> listg = new ArrayList<ListGoodsForm>();
     org.jfree.data.xy.XYSeries series = new org.jfree.data.xy.XYSeries("За месяц:");
     if(!list.isEmpty()){
         lg=list;
         ListGoodsForm listo = new ListGoodsForm();
         java.util.Calendar ca = java.util.Calendar.getInstance();

         for(Good g: list){
             listo.setName(g.getName());
             listo.setMeasure(g.getMeasure());
             listo.setPrice(g.getPrice());
             listo.setQuantity(g.getQuantity());
             listg.add(listo);
             java.util.Date date = g.getDates();
             java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("MMMM");
              ca.setTime(date);
             int in = ca.get(java.util.Calendar.MONTH);
                 String month = format.format(date);
        }
            }
       request.setAttribute("listGood",listg);
     //model.addAttribute("listGood", listg);
     model.addAttribute("GOODS", prepareDataList());
    // model.add
     model.addAttribute("COMPANY",sservice.getSupplierName());
     model.addAttribute("consumptionForm", cmd);

   return "supply";  
}

Answer

JB Nizet picture JB Nizet · Dec 27, 2012

My guess is that your controller is doing the following:

Good g = new Good();
List<Good> goods = new ArrayList<Good>();
for (int i = 0; i < 4; i++) {
    g.setName("a");
    ...
    goods.add(g);
}

This means that you're modifying the same Good object 4 tilmes, and adding it 4 times to the list. In the end, your have 4 times the same object, containing the state you set into it in the last iteration.

Instead, do this:

List<Good> goods = new ArrayList<Good>();
for (int i = 0; i < 4; i++) {
    Good g = new Good();
    g.setName("a");
    ...
    goods.add(g);
}

EDIT : and your edited question just confirmed my guess:

ListGoodsForm listo = new ListGoodsForm();

this line should be inside the for loop, and not outside.