简体   繁体   English

<c:foreach jsp iterate over list

[英]<c:foreach jsp iterate over list

I have searched several examples, still have not get. 我搜索了几个例子,还没有得到。 I am passing an List of GOOD object from controller into jsp pages. 我将GOOD对象​​的List从控制器传递到jsp页面。 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";  
}

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. 这意味着您正在修改相同的Good对象4 tilmes,并将其添加到列表中4次。 In the end, your have 4 times the same object, containing the state you set into it in the last iteration. 最后,您拥有相同对象的4倍,包含您在上一次迭代中设置的状态。

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. 这一行应该在for循环中,而不是在外面。

使用此代码传递列表

 request.setAttribute("listGood",listg);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM