Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<c:foreach jsp iterate over list

Tags:

java

jsp

jsp-tags

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";  
}
like image 704
Olzhas Avatar asked Dec 27 '12 10:12

Olzhas


1 Answers

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.

like image 108
JB Nizet Avatar answered Sep 20 '22 02:09

JB Nizet