< c:foreach jsp迭代列表 [英] <c:foreach jsp iterate over list

查看:99
本文介绍了< c:foreach jsp迭代列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了几个例子,但还没有得到。我将GOOD对象​​的List从控制器传递到jsp页面。试图遍历列表对象,但它只重复显示一个元素。我应该用豆子吗?如果是的话,你能否为我的案例提供更具体的例子。

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";  
}


推荐答案

我的猜测是你的控制器正在执行以下操作:

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);
}

这意味着您正在修改相同的Good object 4 tilmes,并添加它4次到列表中。最后,你有4次相同的对象,包含你在上一次迭代中设置的状态。

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.

相反,这样做:

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();

这一行应该在for循环内,而不是在外面。

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

这篇关于&lt; c:foreach jsp迭代列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆