<c:foreach jsp iterate over list

后端 未结 2 368
迷失自我
迷失自我 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:21

    My guess is that your controller is doing the following:

    Good g = new Good();
    List goods = new ArrayList();
    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 goods = new ArrayList();
    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.

提交回复
热议问题