<c:foreach jsp iterate over list

六月ゝ 毕业季﹏ 提交于 2019-11-29 04:29:14

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.

use this code to pass list

 request.setAttribute("listGood",listg);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!