请教一个JSP下拉列表问题

深蓝的独角仙 2012-03-19 09:21:12
这是一个获取下拉列表项的方法,能够输出所有的列表项,但在jsp页面时却只能显示最后一项
public List<Type> loadPetsType() throws Exception
{
List<Type> list = null;
sql = "SELECT ID, TYPENAME FROM Type ";
try
{
this.pst = this.conn.prepareStatement(sql);
ResultSet rs = this.pst.executeQuery();
while (rs.next())
{
list = new ArrayList<Type>();
type = new Type();
type.setTypeId(rs.getInt(1));
System.out.println(type.getTypeId() + " ");
type.setTypeName(rs.getString(2));
System.out.println(type.getTypeName() + " ");
list.add(type);
}
} catch (Exception e)
{
throw e;
} finally
{
if (this.pst != null)
{
try
{
this.pst.close();
} catch (Exception e)
{
throw e;
}
}
}
return list;
}

jsp页面代码:
<select name="select" class="Common">
<%
List<Type> list = null;
try
{
list = DAOFactory.getIPetsDAOInstance().loadPetsType();
request.setAttribute("typeList", list);
%>
<c:forEach items="${typeList}" var="type">
<option value="${type.typeId}">
${type.typeName }
</option>
</c:forEach>
<%
} catch (Exception e)
{
e.printStackTrace();
}
%>
</select>
请问这是怎么回事?
输出如下:
1
宠物小狗
2
宠物小猪
3
宠物小猫
4
宠物兔
5
宠物鼠
6
宠物猴
7
宠物蛇
16
宠物
jsp页面只显示16 宠物
...全文
137 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hefeihu7511075 2012-03-20
  • 打赏
  • 举报
回复
List<Type> list = new ArrayList<Type>();
sql = "SELECT ID, TYPENAME FROM Type ";
try
{
this.pst = this.conn.prepareStatement(sql);
ResultSet rs = this.pst.executeQuery();
while (rs.next())
{
.....................
hefeihu7511075 2012-03-20
  • 打赏
  • 举报
回复
List<Type> list = new ArrayList<Type>();
sql = "SELECT ID, TYPENAME FROM Type ";
try
{
this.pst = this.conn.prepareStatement(sql);
ResultSet rs = this.pst.executeQuery();
while (rs.next())
{
.....................
hefeihu7511075 2012-03-20
  • 打赏
  • 举报
回复
List<Type> list = new ArrayList<Type>();
sql = "SELECT ID, TYPENAME FROM Type ";
try
{
this.pst = this.conn.prepareStatement(sql);
ResultSet rs = this.pst.executeQuery();
while (rs.next())
{
.....................
上山浦鱼 2012-03-20
  • 打赏
  • 举报
回复
+++++++++++++++++++++ 1
[Quote=引用 8 楼 icebingsky 的回复:]

每次你都重新NEW了一个LIST...也就是它 list = new ArrayList<Type>();


放在while上面。。
[/Quote]
上山浦鱼 2012-03-20
  • 打赏
  • 举报
回复
+++++++++++++++++++++ 1
[Quote=引用 8 楼 icebingsky 的回复:]

每次你都重新NEW了一个LIST...也就是它 list = new ArrayList<Type>();


放在while上面。。
[/Quote]
icebingsky 2012-03-20
  • 打赏
  • 举报
回复
每次你都重新NEW了一个LIST...也就是它 list = new ArrayList<Type>();


放在while上面。。
icebingsky 2012-03-20
  • 打赏
  • 举报
回复
每次你都重新NEW了一个LIST...也就是它 list = new ArrayList<Type>();


放在while上面。。
icebingsky 2012-03-20
  • 打赏
  • 举报
回复
每次你都重新NEW了一个LIST...也就是它 list = new ArrayList<Type>();


放在while上面。。
春风化作秋雨 2012-03-20
  • 打赏
  • 举报
回复
问题的根源是,你初始化是在while循环内,每一次循环都初始化一次,list存放的是最后一次循环的一条记录(也就是最后一条记录)
解决方法:就是3楼的方法,list初始化在while循环之前。
include0000 2012-03-20
  • 打赏
  • 举报
回复
list = new ArrayList<Type>(); 你每次都生成一个新的列表。
dryZeng 2012-03-20
  • 打赏
  • 举报
回复
list = new ArrayList<Type>();放到while外面去。
深蓝的独角仙 2012-03-20
  • 打赏
  • 举报
回复
我就是奇怪,在Java方法里面能输出来,但在页面上就只显示最后一项
MiceRice 2012-03-19
  • 打赏
  • 举报
回复
没看出来问题,你页面上直接右键查看源代码,有生成option么?

81,114

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧