<c:foreach jsp iterate over list

后端 未结 2 362
迷失自我
迷失自我 2020-12-17 00:24

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 showin

相关标签:
2条回答
  • 2020-12-17 01:11

    use this code to pass list

     request.setAttribute("listGood",listg);
    
    0 讨论(0)
  • 2020-12-17 01:21

    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.

    0 讨论(0)
提交回复
热议问题