日期:2014-05-18  浏览次数:20711 次

ArrayList输出问题的问题?
小弟把一个表写到一个ArrayList中
  WareBean wareBean=new WareBean();
  wareBean.setWareID(rs.getString("wareID") );
  wareBean.setWareName(rs.getString("wareName") );
  wareBean.setWarePrice(rs.getInt("warePrice") );
  arr.add(wareBean);

  }
  request.setAttribute("arr",arr);

现在想在jsp页面输出表的内容
应该怎样做啊?


<%
  ArrayList arr=new ArrayList();
  arr = (ArrayList) request.getAttribute("arr");
  for (int i = 0; i < arr.size(); i++) {

  %>
  <tr>
  <td>
  <%=arr.get(i)%>
  </td>
  </tr>

  <% }%>
应该怎样改啊



------解决方案--------------------
问题不清不楚,你上面不都已经把需在页面里的JAVA代码打印出来了,还要做什么
可以在
<body>
<form>
<table border="5">
<tr>
//在下面输出你要打印出的列名,有几项输几项,然后在后台加上一个用来存放动态数组的类
<th>wareID</th>
<th>wareName</th>
<th>warePrice</th>

</tr>
<% 
ArrayList arr=new ArrayList 
arr = (ArrayList) request.getAttribute("arr"); 
for (int i = 0; i < arr.size(); i++) { 
DynamicEntity entity=new DynamicEntity();
%> 
<tr > 
<td><%=entity.get("wareID")% ></td> 
<td><%=entity.get("wareName")% ></td> 
<td><%=entity.get("warePrice")% ></td> 
</tr > 
<% }% > 
</table>
</form>
</body>


在Bean里先建一个DynamicEntity类,代码如下:
public class DynamicEntity(){
private HashMap entity;
public DynamicEntity(){
entity=new HashMap();
}
public void set(String key,String value){
this.entity.put(key,value);
}
public String get(String key){
return (String)enttiy.get(key);
}





------解决方案--------------------
have a try
<% 
ArrayList arr=new ArrayList(); 
arr = (ArrayList) request.getAttribute("arr"); 
for (int i = 0; i < arr.size(); i++) { 
Object obj = arr.get(i);
if (obj instanceof wareBean) {
wareBean wb = (wareBean)obj;
//out.println(wb.getWareID()); so on...
//do something here
}
}
% >